找到你要的答案

Q:Asking for admin privileges for only standard accounts

Q:只要求标准帐户的管理权限

I am moving some files to /Applications directory by my application and using SMJobBless() for Elevating to admin rights privileges.

But it prompts for admin credentials for any type of account- admin or standard_user, can we put a check and ask for admin credentials only for standard users by using any objective-c API?

Edit -- If anyone has tried or gone through process of installing Dropbox, then Dropbox installer does the same(i.e it asks for admin privileges only for standard accounts for copying it's .app file to /Applications).

我把一些文件/应用提升管理员权限,权限由我的应用和使用smjobbless()目录。

但它提示输入管理员凭据的任何类型的帐户管理员或standard_user,才能把支票要求管理员凭据的唯一标准用户使用任何Objective-C API吗?

编辑——如果有人试图安装Dropbox或经历的过程,然后安装Dropbox一样(即它要求管理员权限的唯一标准占复制它的程序文件/应用)。

answer1: 回答1:

Based on the answer of another question, I've made a file that seems to suit you (I've just translated it from C to Objective-C and removed the printf's; the original link is at the end of the answer).

NSPrivileges.h

#import <Foundation/Foundation.h>

@interface NSPrivileges : NSObject

+(BOOL)hasAdminPrivileges;

@end

NSPrivileges.m

#import "NSPrivileges.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <strings.h>
#include <pwd.h>
#include <grp.h>

@implementation NSPrivileges

+(BOOL)hasAdminPrivileges{
    uid_t current_user_id = getuid();
    struct passwd *pwentry = getpwuid(current_user_id);
    struct group *grentry = getgrgid(getgid());

    struct group *admin_group = getgrnam("admin");
    while(*admin_group->gr_mem != NULL) {
        if (strcmp(pwentry->pw_name, *admin_group->gr_mem) == 0) {
            return YES;
        }
        admin_group->gr_mem++;
    }
    return NO;
}

@end

Now, you just need to import NSPrivileges.h to the file which needs to know if the user is an admin or not and use the command [NSPrivileges hasAdminPrivileges] to find out (it will return a BOOL, so it will be easy to deal with). Hope that helps you.

Here is my reference: Objective C and OS user type

基于对一个问题的答案,我做了一个文件,很适合你(我只是翻译从C到Objective-C和移除printf的;原来的链接在最后的答案)。

nsprivileges。H

#import <Foundation/Foundation.h>

@interface NSPrivileges : NSObject

+(BOOL)hasAdminPrivileges;

@end

nsprivileges。M

#import "nsprivileges。H"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <strings.h>
#include <pwd.h>
#include <grp.h>

@implementation NSPrivileges

+(BOOL)hasAdminPrivileges{
    uid_t current_user_id = getuid();
    struct passwd *pwentry = getpwuid(current_user_id);
    struct group *grentry = getgrgid(getgid());

    struct group *admin_group = getgrnam("admin");
    while(*admin_group->gr_mem != NULL) {
        if (strcmp(pwentry->pw_name, *admin_group->gr_mem) == 0) {
            return YES;
        }
        admin_group->gr_mem++;
    }
    return NO;
}

@end

Now, you just need to import nsprivileges。H to the file which needs to know if the user is an admin or not and use the command [NSPrivileges hasAdminPrivileges] to find out (it will return a BOOL, so it will be easy to deal with). Hope that helps you.

Here is my reference: Objective C and OS user type

objective-c  cocoa  security  elevated-privileges  smjobbless