How to get current user's status,guest or administrator?

Discussion in 'Mac Programming' started by MayPeng, Nov 22, 2010.

  1. MayPeng macrumors member

    Nov 21, 2010
    I have a app,and I want to display a warning window if the current user hasn't the admin privilege at the beginning of the app start.So I want to get the current user's identity.
    How can I get the user's identity in a simple way?
  2. HiRez, Nov 23, 2010
    Last edited: Nov 23, 2010

    HiRez macrumors 603


    Jan 6, 2004
    Western US
    You can use the NSUserName() function which returns an NSString. That would allow you to check if they're "guest", but wouldn't necessarily tell you if they have admin privileges. You could also maybe do something like call the UNIX 'id' function from an NSTask, which would show if the user is a member of group 80 (admin). There might be an easier or better way, I'm not sure, but those are a couple options.

    EDIT: On further investigation, it appears there is an Objective-C SFAuthorization class that might do what you want. Check out the obtainWithRight:flags:error: method, which mentions "pre-authorizting", which sounds like it checks if the user has certain privileges.
  3. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    'id' is a command, not a function. A function would be a C function, like getgroups(), which can be called directly rather than being executed through NSTask.

    In any case, the OP's basic premise is flawed. Just because the current user ID doesn't have admin privileges doesn't mean the person using the computer doesn't know the admin password. I usually run as a non-admin user, and only authenticate as needed. The program as described would incorrectly warn me that I'm not an admin user. The OP needs to read "Authorization Services Programming Guide":
  4. MayPeng, Nov 23, 2010
    Last edited by a moderator: Nov 24, 2010

    MayPeng thread starter macrumors member

    Nov 21, 2010
    Thank you for your message.It is very valuable for me.I still have a problem , if I use NSUserName() to have obtained the user name how can I do next step to check whether they are "guest" or "administrator"?

    I am unfamiliar to unix command such as "id",could you please tell me how to show if the user is a member of group 80 (admin) using NSTask.

    Yes, using getgroups() function I can obtain group IDs,but how could it resolve my question? Thank you !

Share This Page