Asking for admin privileges for only standard accounts

464 Views Asked by At

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).

1

There are 1 best solutions below

0
On BEST ANSWER

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