Easiest way to check if user has a specific role?

What’s the easiest way to do

if user.isAdmin() {
// only show if admin
}
struct User {

  func isAdmin() {
  / / code here
  }

}

I’m looking into the ParseSwift samples but I’m still a bit confused on how to implement this best.

Is it just a matter of returning all roles related to the user, and checking if the admin role exists in the returned array?

Is it just a matter of returning all roles related to the user, and checking if the admin role exists in the returned array?

Your function should query the users of role and check if the user is in the returned list. Your isAdmin() method should probably be async, func isAdmin() async throws -> Bool. The query will look similar to the playgrounds:

1 Like

Follow up question @cbaker6,

to get this to work, I had to allow Public FIND on the User class Class Level Permission.

I had it off as I didn’t want it to be publicly searchable.

Is there any security issues doing this you think? Leaving Find on for public is ok?

Public find in CLP being a security/privacy issue depends on your particular app and requirements, so I don’t have much advice there. If you are concerned, you could write the same function in Cloud Code and use the masterKey which will allow you to leave public find off.

right! forgot about that. perfect. :slight_smile: thanks