Get logged in user status

Hello,
How do I know if the logged in user is a standard, administrator or guest user? Is there a Terminal command for this?
Thanks

For Windows: user "%username%"
For macOS and Linux: groups %username%
replace %username% with the username of who you want to check

1 Like

There is a macOS API as well, which may get you faster results (and is more power efficient). I managed to shorten machine fingerprinting from a noticeable 2 second process down to instantly by changing from Shells to Declares.

I know you’re a MBS user, so I went for plugins to simplify implementation. For this, you will need MacFrameworks (which depends on MacCG). Others are welcome to translate this to declares if so desired.

// Get current user
var oCurrentUser as CSIdentityMBS = CSIdentityMBS.CurrentUser

// I'm assuming this means the user is a Guest
// I leave this for you to test
if not oCurrentUser.IsUser then
  // Do Guest stuff
  break
  return
  
end

// Get admin group
var oAdminQuery as CSIdentityQueryMBS = _
CSIdentityQueryMBS.CreateForName("admin", CSIdentityQueryMBS.kCSIdentityQueryStringEquals, _
CSIdentityMBS.kCSIdentityClassGroup, CSIdentityAuthorityMBS.defaultIdentityAuthority)

// Failed to query for admin group
// Catastrophic failure
if not oAdminQuery.Execute then
  break
  return
  
end

// Get the admin identity out of the array
// Intentionally rasises OOBException for error trapping
var _aroAdminQueryResults() as CSIdentityMBS = oAdminQuery.Results
var oAdminGroup as CSIdentityMBS = _aroAdminQueryResults(0)

// Gather the users that belong to the Admin group
var oUserGroupsQuery as CSIdentityQueryMBS = oAdminGroup.GroupMembershipQuery

// Failed to query for admin group users
// Catastrophic failure
if not oUserGroupsQuery.Execute then
  break
  return
  
end

// Look for current user UUID in list of admin users
var sCurrentUUID as String = oCurrentUser.UUID

var aroAdminGroupUsers() as CSIdentityMBS = oUserGroupsQuery.Results
for each oAdminGroupUser as CSIdentityMBS in aroAdminGroupUsers
  var sUserUUID as String = oAdminGroupUser.UUID
  
  if sUserUUID = sCurrentUUID then
    // User is admin
    // Do Admin stuff
    break
    return
    
  end
  
next oAdminGroupUser

// Should the execution reach this point, the user is not an admin
// Do User stuff
break
return

CSIdentityMBS.MemberOfGroup wasn’t working but I’m not sure why so I opted to test the admin members against the current user.

2 Likes

Thanks - you @Tim_Parnell for this great code.
I tested it and it works well for admin and standard users but unfortunately it crashes for guest mode :frowning:

If you can post the version with macOS APIs (declares), I could perform the tests…

Why don’t you just debug, find where the NilObjectException occurs and fix it in your code?

1 Like

Of course I will look.
In the meantime, here is a code (probably slower) that works, for newbies (like me :wink: )

Var sh As New Shell
sh.Execute("id -p")
Var result As String = sh.Result

If result.Contains("admin") Then //or result.IndexOf("admin") > -1 then...
  MsgBox "Current user is an admin."
ElseIf result.Contains("staff") Then
  MsgBox "Current user is a standard user."
Else
  MsgBox "Current user is a guest."
End If

I have a suspicion that NOE might be coming from the plugin design and how I went for the CurrentUser shortcut. I gave you over an hour of my professional expertise researching and building this. I’m sorry but I’m not volunteering to set up a guest user on my machine just to test out a NOE for free.

You haven’t mentioned what line pauses with the exception. To illustrate, that would be the equivalent of “my car won’t take me to the store, what’s wrong with it?”

No mechanic can fix that, and I can’t fix “code broken, why?”

2 Likes

Hi Tim,
I understand. I also think it comes from the Plugin, because despite my tests to remove any reference to the “guest” code, it crashes at launch. If so, maybe the version with API would work. I can’t debug with Xojo because I would have to reinstall everything (Xojo, MBS, etc.) on my Mac’s guest account, which, once left, deletes all data. Thank you for your work, and know that I appreciate the time of expertise that you have devoted to me.

Look at (Xojo folder)/Extras/Remote Debugger Desktop/Remote Debugger Desktop.app and you’ll be able to debug your app in the guest account.

Hi Denis,

I want to apologize for how short I was a few days ago. I think I need to take a break from this place as my fuse seems to be getting shorter and shorter.

I couldn’t sleep on having posted non-working code, so I’ve gone out of my way to flesh out a class that solves the original problem. I could see myself needing this someday.

As a token of my apology, please accept this completed class that will determine on macOS if the user is a Guest, User, or Administrator. I have tested it this time.

Please note the license included in the class. It’s free of charge for literally everyone except AI. Use of my code to train AI is a violation of my rights as the author.

File: clsUser Account Level.xojo_xml_project

Best wishes,
Tim

2 Likes

Take care of yourself, that’s important.

1 Like