留言板

Differentiate users based on their roleId

thumbnail
Genar Codina,修改在7 年前。

Differentiate users based on their roleId

Junior Member 帖子: 28 加入日期: 16-4-18 最近的帖子
The main goal is to develop an app which shows different view controllers depending on the user's roleId (or perhaps another property which differentiate different kind of users).
The user authenticates using e-mail and password.
For example, suppose that we have created two different roles:
1) "Basic User"
2) "Supervisor User"
and we have also created three different users, with the following e-mail addresses:
1)"basicuser@my.host.test" who has the "Basic User" role
2)"supervisoruser@my.host.test" who has the "Supervisor User" role
3) "administratoruser@my.host.test" who has Administrator rights.

I can get the roleId of "basicuser@my.host.test" and "supervisoruser@my.host.test", but only if I use the account with administration permissions and this is something that I would like to avoid.
On the other hand, I cannot get the roleId related to "basicuser@my.host.test" and "supervisoruser@my.host.test" if I use their e-mail and password to authenticate.

In order to get the roleId related to "Basic User" role I call the following method:
let roleWithCompanyId = try roleService.getRoleWithCompanyId(companyIdInt64, name: "Basic User")

But in order to execute the "getRoleWithCompanyId" method I have to authenticate using the account (email/password) which has administrator rights.
Here you are the code that I am using in the ViewController for iOS:
import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // With the basic user authentication I cannot get its own roleId (a permission error occurs)
        // let auth = LRBasicAuthentication(username: "basicuser@my.host.test", password: "test")
        // With the administrator user authentication I can get the role Id of the other users; for example, basic user
        let auth = LRBasicAuthentication(username: "administrator@my.host.test", password: "test")
        let session:LRSession? = LRSession(server: "http://my.host.test", authentication: auth)
        let userService:LRUserService_v62 = LRUserService_v62(session: session)
        let groupService:LRGroupService_v62 = LRGroupService_v62(session: session)
        let roleService:LRRoleService_v62 = LRRoleService_v62(session: session)
        do {
            let userSites = try groupService.getUserSites()
            let userSite = userSites[0]
            let  userSiteDic = userSite as! Dictionary<string, anyobject>
            if let companyId = userSiteDic["companyId"] {
                let numCompanyId = companyId as? NSNumber
                if let companyIdInt64 = numCompanyId?.longLongValue where companyIdInt64 &gt; 0 {
                    print("CompanyId:\(companyIdInt64)")
                    print("--------------")
                    var userAnyObject:NSObject?
                    do {
                        userAnyObject = try userService.getUserByEmailAddressWithCompanyId(companyIdInt64, emailAddress: "basicuser@my.host.test")
                        print(userAnyObject)
                        print("--------------")
                        let userAnyObjectResult = userAnyObject as! Dictionary<string, anyobject>
                        if let userId = userAnyObjectResult["userId"] {
                            let numUserId = userId as? NSNumber
                            if let userIdInt64 = numUserId?.longLongValue where userIdInt64 &gt; 0 {
                                print("UserId:\(userIdInt64)")
                                print("--------------")
                                let roleWithCompanyId = try roleService.getRoleWithCompanyId(companyIdInt64, name: "Basic User")
                                print("RolesWithCompanyId:\(roleWithCompanyId)")
                                let roleId = roleWithCompanyId["roleId"]
                                let numRoleId = roleId as? NSNumber
                                if let numRoleIdInt64 = numRoleId?.longLongValue where numRoleIdInt64 &gt; 0 {
                                    print("RoleId:\(numRoleIdInt64)")
                                    print("--------------")
                                }
                            }
                        }
                        
                    }
                }
            }
        } catch let error as NSError {
            print("Error Occurred ==&gt; \(error)")
        }
    }
}</string,></string,>


I would like to remark that the main purpose is to differentiate if a user who has authenticated (using e-mail/password) is "a basic user" or "a supervisor user". To do so I have tried to get information about its roleId; however, perhaps there is another approach to know the "roleId/category/group/or something else" a user belongs to; and based on that information then the mobile app will be able to present a specific "view" for this "kind of user".
thumbnail
Bruno Farache,修改在7 年前。

RE: Differentiate users based on their roleId (答复)

Liferay Master 帖子: 603 加入日期: 07-5-14 最近的帖子
Genar,

You are probably hitting this code in the server. It's checking if the authenticated user has the VIEW permission over the "Basic User" role.

This user needs permission to see roles. You need to give that permission within Control Panel.
thumbnail
Genar Codina,修改在7 年前。

RE: Differentiate users based on their roleId

Junior Member 帖子: 28 加入日期: 16-4-18 最近的帖子
Thanks Bruno,
In case the admin of the portal is afraid of giving additional permissions I will try to create a custom web service (via SDK Builder) to get this information.