Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Lior Hadaya
Using PermissionServiceUtil's hasUserPermission method
October 28, 2012 4:42 AM
Answer

Lior Hadaya

Rank: Regular Member

Posts: 138

Join Date: January 24, 2012

Recent Posts

Hello,

I am using Liferay EE 6.1.20 and I've been unsuccessfully trying to understand how to use PermissionServiceUtil's hasUserPermissions method:
1 static boolean hasUserPermission(long userId, String actionId, long resourceId)

I understand what userId means, actionId I assume is one of the ActionKeys enum value (like ActionKeys.UPDATE) but what is resourceId?
I know that in Liferay everything from portlet to blog to message board is a resource. Specifically I want to check Update permissions on a layout. I looked in the Resource_ table in the database and found that it is empty. There are rows in the ResourceAction and ResourcePermission tables but I don't see a resourceId column in them.

My question is how can I use hasUserPermission in order to find out if a user has update permission on a layout? The reason I'm interested in this method is because I hope that if I understand how to use it I could also use this method which receives a list or resources in order to check user permissions for several layouts, not just one:
1static boolean hasUserPermissions(long userId, long groupId, List<Resource> resources, String actionId, PermissionCheckerBag permissionCheckerBag)


Please advise,
Thanks
Luca Lupo
RE: Using PermissionServiceUtil's hasUserPermission method
October 28, 2012 5:21 AM
Answer

Luca Lupo

Rank: Regular Member

Posts: 106

Join Date: October 1, 2012

Recent Posts

the resource id is the ID of the resource you want to check the permission for.

For example, a journal article ID if you want to check the permission for that...or in your case the template ID.

So, go on the db (table journalTemplate) and get the ID you need emoticon. I created myself this method if you wanna use it, but it is for journal article. It takes the user and a list of journal articles and check for every role of the user if there is one that can access the journal article.

 1
 2public static boolean hasContentPermission(User utente, JournalArticle webContent) throws SystemException {
 3        //ricavo il companyId del portale
 4         long companyId = CompanyLocalServiceUtil.getCompanies().get(0).getCompanyId();
 5         
 6        long[] listaIdRuoliUtente = null;
 7        listaIdRuoliUtente = utente.getRoleIds();
 8        boolean ruoloTrovato = false;
 9        boolean permessoTrovato = false;
10
11        List<ResourcePermission> rp = new ArrayList<ResourcePermission>();
12         rp = ResourcePermissionLocalServiceUtil.getResourcePermissions(companyId, JournalArticle.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(webContent.getResourcePrimKey()));
13         for (int p=0; p<rp.size(); p++) {
14             ruoloTrovato = false;
15            //ciclo per tutti i ruoli di un determinato utente
16            for (int x=0; x<listaIdRuoliUtente.length; x++) {
17                if (rp.get(p).getRoleId() == listaIdRuoliUtente[x]) ruoloTrovato = true;
18                    if(ruoloTrovato) {
19                        if ((rp.get(p).getActionIds() > 0) && ((rp.get(p).getActionIds()%(2))!=0)) {
20                            permessoTrovato = true;
21                        }
22                    }
23            }
24        }
25         
26        DynamicQuery dynamicQuery = DynamicQueryFactoryUtil
27                   .forClass(ResourcePermission.class)
28                   .add(PropertyFactoryUtil.forName("ownerId")
29                     .eq(utente.getUserId()))
30                   .add(PropertyFactoryUtil.forName("primKey").eq(
31                     String.valueOf(webContent.getResourcePrimKey())));
32       
33        List<ResourcePermission> perm = ResourcePermissionLocalServiceUtil.dynamicQuery(dynamicQuery);
34        if (perm != null || perm.size() > 0) return true;   
35        return permessoTrovato;
36    }



The dynamicQuery is necessary to check the role owner, that is a special role emoticon.

Hope this help,

Luca
Luca Lupo
RE: Using PermissionServiceUtil's hasUserPermission method
October 28, 2012 5:22 AM
Answer

Luca Lupo

Rank: Regular Member

Posts: 106

Join Date: October 1, 2012

Recent Posts

My mistake, the method take a single journalArticle and not a list as I said before
Lior Hadaya
RE: Using PermissionServiceUtil's hasUserPermission method
October 28, 2012 5:28 AM
Answer

Lior Hadaya

Rank: Regular Member

Posts: 138

Join Date: January 24, 2012

Recent Posts

Hi Luca, thanks for replying.

I don't understand why the journalTemplate table is relevant in my case - since I want to check permissions on a layout.
Is resourceId simply the plid of the layout - the primary key of the layout table?

I also don't understand why you had to implement a method that checks all the user's roles for the requested permission, I thought that's what hasUserPermissions does.

Thanks
Luca Lupo
RE: Using PermissionServiceUtil's hasUserPermission method
October 28, 2012 10:23 AM
Answer

Luca Lupo

Rank: Regular Member

Posts: 106

Join Date: October 1, 2012

Recent Posts

Yes, I meant the layout table. primary key should be fine, otherwise check also the other keys in the table.

I was using another function to check the permissions. I don't remember the name, but was a strange function. Basically, it returned a void and you had to check the Exception in order to find out if a user had or not the permission. At the end I implemented my but free version of the function emoticon
Lior Hadaya
RE: Using PermissionServiceUtil's hasUserPermission method
October 29, 2012 1:21 AM
Answer

Lior Hadaya

Rank: Regular Member

Posts: 138

Join Date: January 24, 2012

Recent Posts

I'm sorry but I still don't understand.

If I pass plid as resource id to this method:
hasUserPermission(long userId, String actionId, long resourceId)

How does Liferay know it should check permissions for a layout? How does it know that the long I passed in is a Layout and not a journal or a blog or something else?

I tried passing the plid but the method returned false even though the user does have permission.