[Hippo-cms7-user] about mapping of user, groups and roles

Frank van Lankvelt f.vanlankvelt at onehippo.com
Fri Dec 21 12:58:03 CET 2012


On Fri, Dec 21, 2012 at 6:52 AM, PANKAJ SINGH <er.pankajec12 at gmail.com> wrote:
> hello guys.
> sorry for asking a simple question but i m a new user of hippo so please
> help me out........... i want to know in the following progaram how the user
> groups and roles are mapping ..
>
what exactly do you want to know.  How the concepts 'user', 'group'
and 'role' work together in Hippo CMS?
Or is there a problem in the posted code that you want to be debugged?

For the first question, you could try reading
http://www.onehippo.org/7_8/library/concepts/security/repository-authorization-and-permissions.html
.

cheers, Frank

> package com.mootly.wcm.components.common;
>
> import java.util.Collection;
> import java.util.HashSet;
>
> import javax.jcr.Node;
> import javax.jcr.NodeIterator;
> import javax.jcr.RepositoryException;
> import javax.jcr.Session;
> import javax.jcr.query.Query;
>
> import org.hippoecm.hst.core.component.HstRequest;
> import org.hippoecm.hst.core.component.HstResponse;
> import org.slf4j.Logger;
> import org.slf4j.LoggerFactory;
>
> import com.mootly.wcm.components.BaseComponent;
>
> public class Login extends BaseComponent {
>
>     private final Logger log = LoggerFactory.getLogger(Login.class);
>
>     private final static String SELECT_USER_QUERY = "SELECT * FROM
> hipposys:user WHERE fn:name()='{}'";
>
>     private final static String SELECT_GROUPS_QUERY = "SELECT * FROM
> hipposys:group WHERE jcr:primaryType='hipposys:group' AND
> hipposys:members='{}'";
>
>     @Override
>     public void doBeforeRender(HstRequest request, HstResponse response) {
>         Boolean loggedin = request.getUserPrincipal() != null;
>         request.setAttribute("loggedin", loggedin);
>
>         if (loggedin) {
>             User user = (User) request.getSession().getAttribute("user");
>             if (user == null) {
>                 Session session = null;
>                 try {
>                     session = request.getRequestContext().getSession();
>                     Node userNode = getUserNode(session, request);
>                     if (userNode != null) {
>                         user = new User(userNode, getGroupNodes(session,
> request));
>                         request.getSession().setAttribute("user", user);
>                     }
>                 }
>                 catch (RepositoryException e) {
>                     log.error("Failed to retrieve user from repository", e);
>                 }
>                 finally {
>                     if (session != null) {
>                         session.logout();
>                     }
>                 }
>             }
>             if (user != null) {
>                 request.setAttribute("user", user);
>             }
>         }
>
>         Boolean login = getPublicRequestParameter(request, "login") != null
> && getPublicRequestParameter(request, "login").equals("true");
>         request.setAttribute("login", login);
>
>         Boolean error = getPublicRequestParameter(request, "error") != null
> && getPublicRequestParameter(request, "error").equals("true");
>         request.setAttribute("error", error);
>
>     }
>
>     private static Node getUserNode(Session session, HstRequest request)
> throws RepositoryException {
>         String selectUserStatement = SELECT_USER_QUERY.replace("{}",
> request.getUserPrincipal().getName());
>         @SuppressWarnings("deprecation")
>         Query selectUserQuery =
> session.getWorkspace().getQueryManager().createQuery(selectUserStatement,
> Query.SQL);
>         NodeIterator usersIterator = selectUserQuery.execute().getNodes();
>         if (usersIterator.hasNext()) {
>             return usersIterator.nextNode();
>         }
>         return null;
>     }
>
>     private static NodeIterator getGroupNodes(Session session, HstRequest
> request) throws RepositoryException {
>         String selectGroupsStatement = SELECT_GROUPS_QUERY.replace("{}",
> request.getUserPrincipal().getName());
>         @SuppressWarnings("deprecation")
>         Query selectGroupsQuery =
> session.getWorkspace().getQueryManager().createQuery(selectGroupsStatement,
> Query.SQL);
>         return selectGroupsQuery.execute().getNodes();
>     }
>
>     public static final class User {
>
>         private String firstname = "";
>         private String lastname = "";
>         private Collection<String> groups;
>
>         private User(Node userNode, NodeIterator groupNodes) throws
> RepositoryException {
>             if (userNode.hasProperty("hipposys:firstname")) {
>                 firstname =
> userNode.getProperty("hipposys:firstname").getString();
>             }
>             if (userNode.hasProperty("hipposys:lastname")) {
>                 lastname =
> userNode.getProperty("hipposys:lastname").getString();
>             }
>             groups = new HashSet<String>();
>             while (groupNodes.hasNext()) {
>                 groups.add(groupNodes.nextNode().getName());
>             }
>         }
>
>         public String getFirstname() {
>             return firstname;
>         }
>
>         public String getLastname() {
>             return lastname;
>         }
>
>         public Collection<String> getGroups() {
>             return groups;
>         }
>
>         public boolean isMember(String group) {
>             return groups.contains(group);
>         }
>     }
> }
>
> thank u...
>
> _______________________________________________
> Hippo-cms7-user mailing list and forums
> http://www.onehippo.org/cms7/support/forums.html



--
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
Boston - 1 Broadway, Cambridge, MA 02142

US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
www.onehippo.com


More information about the Hippo-cms7-user mailing list