userroleupdateaction.java

来自「jetspeed源代码」· Java 代码 · 共 375 行 · 第 1/2 页

JAVA
375
字号
            // walk thru all the roles, see if anything changed
            // if changed, update the database
            //
            for (int ix = 0; ix < roles.size(); ix++)
            {
                boolean newValue = rundata.getParameters().getBoolean("box_" + ((Role)roles.get(ix)).getName(), false);
                boolean oldValue = ((Boolean)selected.get(ix + 1)).booleanValue();
                //System.out.println("In role:"+((Role)roles.get(ix)).getName()+" newValue="+newValue+" oldValue="+oldValue);
                if (newValue != oldValue)
                {
                    if (newValue == true)
                    {
                        // grant a role to a user
                        Role newRole = (Role) roles.get(ix);
                        JetspeedSecurity.grantRole( user.getUserName(), newRole.getName());

                        // If role profile merging is active, append profile for the new role
                        if (Profiler.useRoleProfileMerging())
                        {
                            appendNewRoleProfile((JetspeedRunData) rundata, user, newRole);
                        }
                    }
                    else
                    {
                        // revoke a role from a user
                        JetspeedSecurity.revokeRole( user.getUserName(),
                                                    ((Role)roles.get(ix)).getName() );
                    }
                }
            }

            // clear the temp values
            rundata.getUser().setTemp(SecurityConstants.CONTEXT_ROLES, null);
            rundata.getUser().setTemp(SecurityConstants.CONTEXT_SELECTED, null);

        }
        catch (Exception e)
        {
           //  the error msg
            logger.error("Failed update role+permission: ", e);
            //
            // error on update - display error message
            //
            DynamicURI duri = new DynamicURI (rundata);
            duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_USERROLE_UPDATE);
            duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_UPDATE_FAILED);
            if (user != null)
                duri.addPathInfo(SecurityConstants.PARAM_ENTITY_ID, user.getUserName());
            rundata.setRedirectURI(duri.toString());

        }
    }

    /**
     * Appends profile for specified role to the end of profile for specified user
     * 
     * @param user   User to append to
     * @param role   Role to append from
     * @exception Exception
     */
    private void appendNewRoleProfile(JetspeedRunData jdata, JetspeedUser user, Role role)
    throws Exception
    {
        // Retrieve the role profile
        ProfileLocator roleLocator = Profiler.createLocator();
        roleLocator.setRole(role);
        roleLocator.setMediaType(jdata.getCapability().getPreferredMediaType());
        roleLocator.setName("default.psml");
        Profile roleProfile = Profiler.getProfile(roleLocator);
        if (roleProfile != null)
        {
            if (logger.isDebugEnabled())
            {
                logger.debug("UserRoleUpdateAction: retrieved profile for role: " + roleProfile.getPath());
            }
        }

        // Retrieve the user profile
        ProfileLocator userLocator = Profiler.createLocator();
        userLocator.setUser(user);
        userLocator.setMediaType(jdata.getCapability().getPreferredMediaType());
        userLocator.setName("default.psml");
        Profile userProfile = Profiler.getProfile(userLocator);
        if (userProfile != null)
        {
            if (logger.isDebugEnabled())
            {
                logger.debug("UserRoleUpdateAction: retrieved profile for user: " + userProfile.getPath());
            }
        }

        // Append role profile to user profile
        if (roleProfile != null && 
            roleProfile.getDocument() != null && 
            userProfile != null && 
            userProfile.getDocument() != null)
        {
            Profile tmpProfile = (Profile) roleProfile.clone();
            Portlets rolePortlets = tmpProfile.getDocument().getPortlets();
            Portlets userPortlets = userProfile.getDocument().getPortlets();

            // Handle pane based profile
            if (rolePortlets.getPortletsCount() > 0)
            {
                for (int i = 0; i < rolePortlets.getPortletsCount(); i++)
                {
                    Portlets pane = rolePortlets.getPortlets(i);
                    pane.setLayout(null);                            
                    userPortlets.addPortlets(pane);
                    if (logger.isDebugEnabled())
                    {
                        logger.debug("UserRoleUpdateAction: appended pane: " + pane.getId() + " to user: " + user.getUserName());
                    }
                }
            }
            // Handle profile with no panes
            else
            {
                if (rolePortlets.getTitle() == null)
                {
                    String title = org.apache.turbine.util.StringUtils.firstLetterCaps(roleProfile.getRoleName());
                    rolePortlets.setTitle(title + " Home");
                }
                rolePortlets.setLayout(null);
                userPortlets.addPortlets(rolePortlets);
            }

            // Regenerate ids
            PortletUtils.regenerateIds(userPortlets);

            // Save the user profile
            PsmlManager.store(userProfile);
        }
    }

    /**
     * Build the context for a role browser for a specific user.
     *
     * @param portlet The velocity-based portlet that is being built.
     * @param context The velocity context for this request.
     * @param rundata The turbine rundata context for this request.
     * @param userid The userid of the user that we are building a role context for.
     */
    private void buildUserRoleContext(VelocityPortlet portlet,
                                       Context context,
                                       RunData rundata,
                                       String userid)
        throws Exception
    {
        // get the user object
        JetspeedUser user = JetspeedSecurity.getUser(userid);
        if (null == user)
        {
            // no User found
            logger.error("UserRoleBrowser: Failed to get user: " + userid );
            return;
        }
        // get master list of roles
        Iterator roles = JetspeedSecurity.getRoles();
        Vector masterRoles = new Vector();
        Vector selected = new Vector();
        int ix = 0;
        boolean sel = false;
        selected.add(ix, new Boolean(sel));
        while(roles.hasNext())
        {
            Role role = (Role)roles.next();
            //System.out.println("In buildUserRoleContext role="+role.getName());
            masterRoles.add(role);
            sel = JetspeedSecurity.hasRole(user.getUserName(), role.getName());
            //System.out.println("In buildUserRoleContext sel="+sel);
            ix = ix + 1;
            selected.add(ix, new Boolean(sel));
        }
        masterRoles.trimToSize();
        selected.trimToSize();

        rundata.getUser().setTemp(SecurityConstants.CONTEXT_ROLES, masterRoles);
        rundata.getUser().setTemp(SecurityConstants.CONTEXT_SELECTED, selected);
        context.put(SecurityConstants.CONTEXT_USER, user);
        context.put(SecurityConstants.CONTEXT_ROLES, masterRoles);
        context.put(SecurityConstants.CONTEXT_SELECTED, selected);

    }


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?