Thanks a lot everybody for your help, it has been very useful, finally it
works with the next code:

protected CoreSession getSystemSession() throws Exception {
        String repositoryName = "default";
        try {
            return CoreInstance.getInstance().open(repositoryName, null);
        } catch (ClientException e) {
            throw new NuxeoJbpmException(e);
        }
    }


    public void assignReadPermissions(DocumentModel doc) throws Exception {
            CoreSession session = null;
            try {
                session = getSystemSession();
                DocumentRef docRef = doc.getRef();
                ACP acp = session.getACP(docRef);
                ACL acl = acp.getOrCreateACL();
                acl.add(new ACE("dictamen_readers", SecurityConstants.READ,
true));
                acp.addACL(acl);
                AddRightUnrestricted runner = new
AddRightUnrestricted(session, docRef, acp);
                runner.runUnrestricted();
            } finally {
                if (session != null) {
                    CoreInstance.getInstance().close(session);
                }
            }
    }


Best regards
Jesus



On Tue, Dec 7, 2010 at 11:35 AM, Florent Guillaume <[email protected]> wrote:

> You should never log in manually. Just subclass
> UnrestrictedSessionRunner, implement run() and call runUnrestricted().
>
> Florent
>
> On Tue, Dec 7, 2010 at 5:46 PM, Jesus Ibañez <[email protected]>
> wrote:
> > Thanks a lot for your rapid response, i have try to log in as follows but
> it
> > does not work:
> >
> >
> > try {
> >            LoginContext lc = Framework.login(Administrator, pass);
> >                session.setACP(doc.getRef(), acp, true);
> >            lc.logout();
> >         } catch( ClientException e ) {
> >             e.printStackTrace();
> >     }
> >
> > any clue?
> >
> >
> > On Tue, Dec 7, 2010 at 10:30 AM, Bruce Grant <[email protected]>
> wrote:
> >>
> >> You could log in with administrator privileges (or with an account that
> >> has WriteSecurity assigned) in your listener.
> >>
> >> regards,
> >> Bruce.
> >>
> >>
> >>
> >> From:        Jesus Ibañez <[email protected]>
> >> To:        ECM Nuxeo <[email protected]>
> >> Date:        12/07/2010 11:26 AM
> >> Subject:        [Ecm] how to assign permissions to a user group via a
> >> listener through a system session
> >> ________________________________
> >>
> >>
> >>
> >> Hello!
> >>
> >> I have trying to assign permissions to a users group on a document via
> >> listener, it works fine if the signed user is the Administrator but if
> is
> >> other (for instance "user.test") it sends an error like follows:
> >>
> >>  ERROR [STDERR] org.nuxeo.ecm.core.api.RollbackClientException:
> >> org.nuxeo.ecm.core.api.WrappedException: Exception:
> >> org.nuxeo.ecm.core.api.DocumentSecurityException. message: Privilege
> >> 'WriteSecurity' is not granted to 'user.test'
> >>
> >> the user "user.test" does not must to have  privileges of
> >> 'WriteSecurity', so i have seen maybe i have to get a system session
> before
> >> changing any privilege, could someone help me on how to implement this?
> >>
> >>
> >> my listener looks as follows:
> >>
> >> package com.mydomain.tramite;
> >>
> >> import org.nuxeo.ecm.core.api.ClientException;
> >> import org.nuxeo.ecm.core.api.CoreSession;
> >> import org.nuxeo.ecm.core.api.DocumentModel;
> >> import org.nuxeo.ecm.core.event.Event;
> >> import org.nuxeo.ecm.core.event.EventContext;
> >> import org.nuxeo.ecm.core.event.EventListener;
> >> import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
> >> import org.nuxeo.ecm.core.api.NuxeoPrincipal;
> >> import org.nuxeo.ecm.core.api.security.ACE;
> >> import org.nuxeo.ecm.core.api.security.ACL;
> >> import org.nuxeo.ecm.core.api.security.ACP;
> >> import org.nuxeo.ecm.core.api.security.SecurityConstants;
> >>
> >> public class AssignReadPermissions implements EventListener {
> >>
> >>     public void handleEvent(Event event) throws ClientException {
> >>
> >>         EventContext ctx = event.getContext();
> >>
> >>      // Get currentUser
> >>         NuxeoPrincipal currentUser = (NuxeoPrincipal)
> >> event.getContext().getPrincipal();
> >>
> >>         if (ctx instanceof DocumentEventContext) {
> >>
> >>             DocumentEventContext docCtx = (DocumentEventContext) ctx;
> >>             DocumentModel doc = docCtx.getSourceDocument();
> >>             CoreSession session = docCtx.getCoreSession();
> >>
> >>             if (doc != null) {
> >>                 String type = doc.getType();
> >>                 if ("tramite".equals(type)) {
> >>                     assignReadPermissions(doc, session);
> >>                 }
> >>             }
> >>         }
> >>     }
> >>
> >>     protected void assignReadPermissions(DocumentModel doc, CoreSession
> >> session) throws ClientException {
> >>         ACP acp = doc.getACP();
> >>         ACL myACL = doc.getACP().getOrCreateACL();
> >>         ACE myACE = new ACE("dictamen_readers", SecurityConstants.READ,
> >> true);
> >>         myACL.add(myACE);
> >>         try {
> >>             session.setACP(doc.getRef(), acp, true);
> >>         } catch( ClientException e ) {
> >>             e.printStackTrace();
> >>     }
> >>
> >> }
> >>
> >> }
> >>
> >>
> >> Any help would be appreciated!
> >>
> >> --
> >> Jesus_______________________________________________
> >> ECM mailing list
> >> [email protected]
> >> http://lists.nuxeo.com/mailman/listinfo/ecm
> >> To unsubscribe, go to http://lists.nuxeo.com/mailman/options/ecm
> >>
> >
> >
> >
> > --
> > Jesus
> >
> > _______________________________________________
> > ECM mailing list
> > [email protected]
> > http://lists.nuxeo.com/mailman/listinfo/ecm
> > To unsubscribe, go to http://lists.nuxeo.com/mailman/options/ecm
> >
> >
>
>
>
> --
> Florent Guillaume, Director of R&D, Nuxeo
> Open Source, Java EE based, Enterprise Content Management (ECM)
> http://www.nuxeo.com   http://www.nuxeo.org   +33 1 40 33 79 87
>



-- 
Jesus
_______________________________________________
ECM mailing list
[email protected]
http://lists.nuxeo.com/mailman/listinfo/ecm
To unsubscribe, go to http://lists.nuxeo.com/mailman/options/ecm

Reply via email to