Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Alexey Kakunin
Setting default Permission for USER
October 9, 2009 8:40 AM
Answer

Alexey Kakunin

Rank: Expert

Posts: 363

Join Date: July 7, 2008

Recent Posts

Hi!
I need to change default permission for BlogEntry for USER - to allow all users to add comments on blog - not only community members (by default only community members has ADD_DISCUSSION permission).

Problem in - in resource-actions/blog.xml - for BlogEntry I can set guest-default, community-default - but there are no user-default.

Any way to change it via config-files (not in the Code)?
rudy baer
RE: Setting default Permission for USER
February 3, 2010 12:44 PM
Answer

rudy baer

Rank: New Member

Posts: 2

Join Date: June 18, 2009

Recent Posts

Hi!
I would like to do the same thing for my own portlet.
Did you find a way?
Alexey Kakunin
RE: Setting default Permission for USER
February 4, 2010 12:49 AM
Answer

Alexey Kakunin

Rank: Expert

Posts: 363

Join Date: July 7, 2008

Recent Posts

rudy baer:
Hi!
I would like to do the same thing for my own portlet.
Did you find a way?


Hi!
No, I did not found how to do it via configuration files, so, I've implemented required assigning of permissions to required role in my java-code
rudy baer
RE: Setting default Permission for USER
February 9, 2010 12:38 AM
Answer

rudy baer

Rank: New Member

Posts: 2

Join Date: June 18, 2009

Recent Posts

ok thank you
Gordon Augat
RE: Setting default Permission for USER
April 27, 2010 10:30 AM
Answer

Gordon Augat

Rank: Junior Member

Posts: 89

Join Date: August 16, 2006

Recent Posts

Where did you put your custom code? Did you override a core liferay class in the ext environment or did you use a hook? Can you provide some pseudo code of what you did? Thanks!
Alexey Kakunin
RE: Setting default Permission for USER
April 28, 2010 4:37 AM
Answer

Alexey Kakunin

Rank: Expert

Posts: 363

Join Date: July 7, 2008

Recent Posts

Hi!
I fixed it in ext-environment (Liferay 5 is not allowed to change liferay's java core via hooks).
Source code available at SVN - just search for ADD_DISCUSSION

Hope it will help
Gordon Augat
RE: Setting default Permission for USER
April 29, 2010 9:41 AM
Answer

Gordon Augat

Rank: Junior Member

Posts: 89

Join Date: August 16, 2006

Recent Posts

Thanks. I ended up creating a BlogListener class and put your code to add the permissions in the onAfterCreate and onAfterUpdate methods. Works great!

 1public class BlogListener extends BaseModelListener<BlogsEntry> {
 2    private static Log _log = LogFactoryUtil.getLog(BlogListener.class);
 3
 4    public void onAfterCreate(BlogsEntry entry) throws ModelListenerException {
 5        updatePermissions(entry);
 6    }
 7
 8    public void onAfterRemove(BlogsEntry entry) throws ModelListenerException {
 9    }
10
11    public void onAfterUpdate(BlogsEntry entry) throws ModelListenerException {
12        updatePermissions(entry);
13    }
14
15    public void onBeforeCreate(BlogsEntry entry) throws ModelListenerException {
16    }
17
18    public void onBeforeRemove(BlogsEntry entry) throws ModelListenerException {
19    }
20
21    public void onBeforeUpdate(BlogsEntry entry) throws ModelListenerException {
22    }
23
24    private void updatePermissions(BlogsEntry entry) throws ModelListenerException {
25        try {
26            Resource resource = ResourceLocalServiceUtil.addResource(entry.getCompanyId(), BlogsEntry.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(entry
27                    .getEntryId()));
28            List<Permission> userPermissionsList = PermissionLocalServiceUtil.getPermissions(entry.getCompanyId(), new String[] { "ADD_DISCUSSION" }, resource.getResourceId());
29            Role userRole = RoleLocalServiceUtil.getRole(entry.getCompanyId(), RoleConstants.USER);
30
31            RoleUtil.addPermissions(userRole.getRoleId(), userPermissionsList);
32        } catch (Exception e) {
33            throw new ModelListenerException(e);
34        }
35    }
36}
Alexey Kakunin
RE: Setting default Permission for USER
April 30, 2010 1:21 AM
Answer

Alexey Kakunin

Rank: Expert

Posts: 363

Join Date: July 7, 2008

Recent Posts

Hi!

Nice to hear it helps.
And yes, Model Listener is a good way to do it via Hooks.
In our case we anyway used ext for blogs, since needed to sanytize entered html and fix another bug with permissions.

===
Alexey Kakunin
Liferay Based Project Hosting: www.emforge.net
lsli lsli
RE: Setting default Permission for USER
August 5, 2011 11:07 PM
Answer

lsli lsli

Rank: Junior Member

Posts: 56

Join Date: November 10, 2008

Recent Posts

Hey guys,

Thanks for this thread! I was able to get this to work for me, albeit with slightly different code.

Rudy - your code provided a good framework for me to start, but I wasn't able to get it to work with your code. Perhaps it's because I'm using Liferay 6.0.6 GA, but here's what I modified (just the updatePermissions method) I have:

 1private void updatePermissions(BlogsEntry entry)
 2            throws ModelListenerException {
 3        try {
 4            ResourcePermissionLocalServiceUtil.setResourcePermissions(
 5                    entry.getCompanyId(),
 6                    BlogsEntry.class.getName(),
 7                    ResourceConstants.SCOPE_INDIVIDUAL,
 8                    Long.toString(entry.getPrimaryKey()),
 9                    RoleLocalServiceUtil.getRole(entry.getCompanyId(),
10                            RoleConstants.USER).getRoleId(), new String[] {
11                            "ADD_DISCUSSION", "VIEW" });
12        } catch (Exception e) {
13            throw new ModelListenerException(e);
14        }
15    }


Hopefully, others will find this useful, too.
Ivano Carrara
RE: Setting default Permission for USER
September 26, 2011 10:13 AM
Answer

Ivano Carrara

Rank: Regular Member

Posts: 225

Join Date: July 2, 2005

Recent Posts

I'm looking for the right path to programmatically assign a permission on a single record of a table to a specified User, if he/she actually don't have it.

Below the example:

1<c:if test="<%= !permissionChecker.hasPermission(groupId, Test.class.getName(), testPrimaryKey, "ADD") %>">
2        <CODE-TO-ASSIGN-THE ADD-PERMISSION-TO-THE-USER>
3    </c:if>


Please, any help?

Thank you in advance!

Ivano C.