cancel
Showing results for 
Search instead for 
Did you mean: 

Constraint - Getting current user

matjazmuhic
Champ on-the-rise
Champ on-the-rise
So, here's the thing…

I have implemented custom lucene search based constraint in Java where I used admin:admin to for authentication with AuthenticationService. Now I'm wondering how could I get current logged-in user and check if the user has permissions to read the nodes returned from lucene search.

I tried some things but I think it didn't work cause constraint class is loaded before anyone is logged in so there's no current user. Is it ok to just skip everything until I can get current user?

Can I get current user from my custom constraint or should I implement this using maybe jsf managed bean (just my blind guess) or any other way?
2 REPLIES 2

savic_prvoslav
Champ on-the-rise
Champ on-the-rise
NodeRef person = Application.getCurrentUser(
      FacesContext.getCurrentInstance()).getPerson();
   String username = (String) Repository.getServiceRegistry(
      FacesContext.getCurrentInstance()).getNodeService()
      .getProperty(person, ContentModel.PROP_USER_USERNAME);

   AuthenticationUtil.runAs(new RunAsWork<Boolean>() {

       @Override
       public Boolean doWork() throws Exception {
      return Repository.getServiceRegistry(
         FacesContext.getCurrentInstance())
         .getPermissionService().hasPermission("PUT NODE REF WITHCK YOU ARE CHECKING",
            PermissionService.DELETE);
       }
   }, username);

hope this helps.

matjazmuhic
Champ on-the-rise
Champ on-the-rise
Thank you for the help. But I'm new to this and I'm still wondering if all this (including the constraint) should be implemented in constraint java class or do I implement everything in JSF bean or do I implement constraint in java class and this functionality in jsf bean?

All those options and largeness of alfresco can be overwhelming to a newbie most of the times.. Smiley Happy



Problem solved. I did that in constraint class. So all the work is done as currently logged in user.