Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Nivas Kulukuri
How to add role permission to a DLFileEntry or a document uploaded
October 31, 2012 4:24 AM
Answer

Nivas Kulukuri

Rank: New Member

Posts: 13

Join Date: October 14, 2012

Recent Posts

Hi,
Please help me out. I am struck in middle of a program.
I am uploading a document using DLFileEntry but at the same time i want to set role permissions to the uploaded document.
I am unable to set permissions.


How to set permissions to a document or DLFileEntry using Liferay API 6.1??
Pavel Savinov
RE: How to add role permission to a DLFileEntry or a document uploaded
October 31, 2012 4:47 AM
Answer

Pavel Savinov

Rank: Junior Member

Posts: 97

Join Date: September 24, 2012

Recent Posts

Hi!

You can use PermissionLocalServiceUtil (setRolePermission or setRolePermissions methods, look portal's APIdocs) to add a permission to the resource represented by your DLFileEntry. To get resourceId use ResourceLocalServiceUtil.

1long resourceId = ResourceLocalServiceUtil.getResource(fileEntry.getCompanyId(),
2                    DLFileEntry.class.getName(),
3                    ResourceConstants.SCOPE_INDIVIDUAL,
4                    String.valueOf(fileEntry.getFileEntryId())).getResourceId();
5String[] actionsRW = new String[]{ActionKeys.VIEW, ActionKeys.UPDATE};
6long roleId = 0L; /* obtain a roleId here */
7PermissionLocalServiceUtil.setRolePermissions(roleId, actionsRW, resourceId);
Jitendra Rajput
RE: How to add role permission to a DLFileEntry or a document uploaded
October 31, 2012 4:54 AM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 815

Join Date: January 7, 2011

Recent Posts

If you are using LR5.X then you can use solution proposed by Pavel . In case if you are using 6.X then use below code.

 1       ResourcePermission resourcePermission = null;
 2            try
 3            {
 4                resourcePermission = ResourcePermissionLocalServiceUtil.getResourcePermission(companyId,
 5                        DLFileEntry.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(fileEntry
 6                                .getPrimaryKey()), roleId);
 7
 8             
 9                if (Validator.isNotNull(resourcePermission))
10                {
11                
12                    resourcePermission.setActionIds(actionIds);
13                    ResourcePermissionLocalServiceUtil.updateResourcePermission(resourcePermission);
14                }
15            } catch (com.liferay.portal.NoSuchResourcePermissionException e)
16            {
17
18                resourcePermission = ResourcePermissionLocalServiceUtil
19                        .createResourcePermission(CounterLocalServiceUtil.increment());
20                resourcePermission.setCompanyId(companyId);
21                resourcePermission.setName(DLFileEntry.class.getName());
22                resourcePermission.setScope(ResourceConstants.SCOPE_INDIVIDUAL);
23                resourcePermission.setPrimKey(String.valueOf(fileEntry.getPrimaryKey()));
24                resourcePermission.setRoleId(roleId);
25                resourcePermission.setActionIds(1);// (ActionKeys.VIEW);
26                ResourcePermissionLocalServiceUtil.addResourcePermission(resourcePermission);
27            }
Pavel Savinov
RE: How to add role permission to a DLFileEntry or a document uploaded
October 31, 2012 5:00 AM
Answer

Pavel Savinov

Rank: Junior Member

Posts: 97

Join Date: September 24, 2012

Recent Posts

Thanks Jitendra! I'll write it down for the future use...
And will the code with PermissionLocalServiceUtil work in 6.1? Or it's necessary to refactor old code to use ResourcePermissionLocalServiceUtil for the DLFileEntry objects?
Jitendra Rajput
RE: How to add role permission to a DLFileEntry or a document uploaded
October 31, 2012 5:00 AM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 815

Join Date: January 7, 2011

Recent Posts

Same code i used in LR6.1 GA2 for same use case and it is working for me. emoticon
Nivas Kulukuri
RE: How to add role permission to a DLFileEntry or a document uploaded
October 31, 2012 5:41 AM
Answer

Nivas Kulukuri

Rank: New Member

Posts: 13

Join Date: October 14, 2012

Recent Posts

Pavel Savinov:
Hi!

You can use PermissionLocalServiceUtil (setRolePermission or setRolePermissions methods, look portal's APIdocs) to add a permission to the resource represented by your DLFileEntry. To get resourceId use ResourceLocalServiceUtil.

1long resourceId = ResourceLocalServiceUtil.getResource(fileEntry.getCompanyId(),
2                    DLFileEntry.class.getName(),
3                    ResourceConstants.SCOPE_INDIVIDUAL,
4                    String.valueOf(fileEntry.getFileEntryId())).getResourceId();
5String[] actionsRW = new String[]{ActionKeys.VIEW, ActionKeys.UPDATE};
6long roleId = 0L; /* obtain a roleId here */
7PermissionLocalServiceUtil.setRolePermissions(roleId, actionsRW, resourceId);



HI
In this way i am getting resourceId=0. Its not working
Pavel Savinov
RE: How to add role permission to a DLFileEntry or a document uploaded
October 31, 2012 6:22 AM
Answer

Pavel Savinov

Rank: Junior Member

Posts: 97

Join Date: September 24, 2012

Recent Posts

Nivas Kulukuri:

HI
In this way i am getting resourceId=0. Its not working


Yep, as Jitendra said, it'll work only in 5... there was already such kind of problem:
http://www.liferay.com/community/forums/-/message_boards/message/6457146
Jitendra Rajput
RE: How to add role permission to a DLFileEntry or a document uploaded
October 31, 2012 6:26 AM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 815

Join Date: January 7, 2011

Recent Posts

In Liferay 5.x details were getting stored in resource_ table but now in LR 6.x they are storing in resourcePermission table thats the reason you are getting 0 as a result.
Nivas Kulukuri
RE: How to add role permission to a DLFileEntry or a document uploaded
October 31, 2012 7:15 AM
Answer

Nivas Kulukuri

Rank: New Member

Posts: 13

Join Date: October 14, 2012

Recent Posts

Jitendra Rajput:
If you are using LR5.X then you can use solution proposed by Pavel . In case if you are using 6.X then use below code.

 1       ResourcePermission resourcePermission = null;
 2            try
 3            {
 4                resourcePermission = ResourcePermissionLocalServiceUtil.getResourcePermission(companyId,
 5                        DLFileEntry.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(fileEntry
 6                                .getPrimaryKey()), roleId);
 7
 8             
 9                if (Validator.isNotNull(resourcePermission))
10                {
11                
12                    resourcePermission.setActionIds(actionIds);
13                    ResourcePermissionLocalServiceUtil.updateResourcePermission(resourcePermission);
14                }
15            } catch (com.liferay.portal.NoSuchResourcePermissionException e)
16            {
17
18                resourcePermission = ResourcePermissionLocalServiceUtil
19                        .createResourcePermission(CounterLocalServiceUtil.increment());
20                resourcePermission.setCompanyId(companyId);
21                resourcePermission.setName(DLFileEntry.class.getName());
22                resourcePermission.setScope(ResourceConstants.SCOPE_INDIVIDUAL);
23                resourcePermission.setPrimKey(String.valueOf(fileEntry.getPrimaryKey()));
24                resourcePermission.setRoleId(roleId);
25                resourcePermission.setActionIds(1);// (ActionKeys.VIEW);
26                ResourcePermissionLocalServiceUtil.addResourcePermission(resourcePermission);
27            }



Thanks... it worked like a charm
Jitendra Rajput
RE: How to add role permission to a DLFileEntry or a document uploaded
October 31, 2012 7:24 AM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 815

Join Date: January 7, 2011

Recent Posts

Good to know that emoticon
Riley Pickerl
RE: How to add role permission to a DLFileEntry or a document uploaded
March 4, 2013 1:49 PM
Answer

Riley Pickerl

Rank: Junior Member

Posts: 50

Join Date: January 10, 2013

Recent Posts

Hey, great info.
Just one question. Where are you adding the method listed above? We are trying to add similair functionality to LR 6.1 ee ga2, using a hook.
I am not sure if I should be hooking a service or customizing a listener.
Could you provide the steps for how to implement this modification?
I am new to hooks and would be every interested in knowing how this is done.

Thanks in advance!
Riley Pickerl

Jitendra Rajput:
If you are using LR5.X then you can use solution proposed by Pavel . In case if you are using 6.X then use below code.

 1       ResourcePermission resourcePermission = null;
 2            try
 3            {
 4                resourcePermission = ResourcePermissionLocalServiceUtil.getResourcePermission(companyId,
 5                        DLFileEntry.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(fileEntry
 6                                .getPrimaryKey()), roleId);
 7
 8             
 9                if (Validator.isNotNull(resourcePermission))
10                {
11                
12                    resourcePermission.setActionIds(actionIds);
13                    ResourcePermissionLocalServiceUtil.updateResourcePermission(resourcePermission);
14                }
15            } catch (com.liferay.portal.NoSuchResourcePermissionException e)
16            {
17
18                resourcePermission = ResourcePermissionLocalServiceUtil
19                        .createResourcePermission(CounterLocalServiceUtil.increment());
20                resourcePermission.setCompanyId(companyId);
21                resourcePermission.setName(DLFileEntry.class.getName());
22                resourcePermission.setScope(ResourceConstants.SCOPE_INDIVIDUAL);
23                resourcePermission.setPrimKey(String.valueOf(fileEntry.getPrimaryKey()));
24                resourcePermission.setRoleId(roleId);
25                resourcePermission.setActionIds(1);// (ActionKeys.VIEW);
26                ResourcePermissionLocalServiceUtil.addResourcePermission(resourcePermission);
27            }
Ashok Kumar
RE: How to add role permission to a DLFileEntry or a document uploaded
January 8, 2015 9:38 AM
Answer

Ashok Kumar

Rank: Junior Member

Posts: 25

Join Date: November 16, 2011

Recent Posts

Jitendra Rajput:
If you are using LR5.X then you can use solution proposed by Pavel . In case if you are using 6.X then use below code.

 1       ResourcePermission resourcePermission = null;
 2            try
 3            {
 4                resourcePermission = ResourcePermissionLocalServiceUtil.getResourcePermission(companyId,
 5                        DLFileEntry.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(fileEntry
 6                                .getPrimaryKey()), roleId);
 7
 8             
 9                if (Validator.isNotNull(resourcePermission))
10                {
11                
12                    resourcePermission.setActionIds(actionIds);
13                    ResourcePermissionLocalServiceUtil.updateResourcePermission(resourcePermission);
14                }
15            } catch (com.liferay.portal.NoSuchResourcePermissionException e)
16            {
17
18                resourcePermission = ResourcePermissionLocalServiceUtil
19                        .createResourcePermission(CounterLocalServiceUtil.increment());
20                resourcePermission.setCompanyId(companyId);
21                resourcePermission.setName(DLFileEntry.class.getName());
22                resourcePermission.setScope(ResourceConstants.SCOPE_INDIVIDUAL);
23                resourcePermission.setPrimKey(String.valueOf(fileEntry.getPrimaryKey()));
24                resourcePermission.setRoleId(roleId);
25                resourcePermission.setActionIds(1);// (ActionKeys.VIEW);
26                ResourcePermissionLocalServiceUtil.addResourcePermission(resourcePermission);
27            }



I modified your code into a reusable method with the Resource Action Permissions. Haven't tested it yet.
I don't think it will pose any issues.
 1
 2  public static void setFilePermissions(FileEntry fileEntry) throws Exception{
 3    ResourcePermission resourcePermission = null;
 4    final Role siteMemberRole = RoleLocalServiceUtil.getRole(fileEntry.getCompanyId(), RoleConstants.SITE_MEMBER);
 5    ResourceAction resourceAction = ResourceActionLocalServiceUtil.getResourceAction(DLFileEntry.class.getName(), ActionKeys.VIEW);
 6    try
 7    {
 8        resourcePermission = ResourcePermissionLocalServiceUtil.getResourcePermission(fileEntry.getCompanyId(),
 9                DLFileEntry.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(fileEntry
10                        .getPrimaryKey()), siteMemberRole.getRoleId());
11
12      
13        if (Validator.isNotNull(resourcePermission))
14        {
15         
16            resourcePermission.setActionIds(resourceAction.getBitwiseValue());
17            ResourcePermissionLocalServiceUtil.updateResourcePermission(resourcePermission);
18        }
19    } catch (com.liferay.portal.NoSuchResourcePermissionException e)
20    {
21
22        resourcePermission = ResourcePermissionLocalServiceUtil
23                .createResourcePermission(CounterLocalServiceUtil.increment());
24        resourcePermission.setCompanyId(fileEntry.getCompanyId());
25        resourcePermission.setName(DLFileEntry.class.getName());
26        resourcePermission.setScope(ResourceConstants.SCOPE_INDIVIDUAL);
27        resourcePermission.setPrimKey(String.valueOf(fileEntry.getPrimaryKey()));
28        resourcePermission.setRoleId(siteMemberRole.getRoleId());
29        resourcePermission.setActionIds(resourceAction.getBitwiseValue());// (ActionKeys.VIEW);
30        ResourcePermissionLocalServiceUtil.addResourcePermission(resourcePermission);
31    }
32  }


Reference: http://itsliferay.blogspot.in/2012/11/add-resource-permission-in-lr-61.html