开发者

How can I set the User Information in jetspeed?

We are using Jetspeed in a project and have a requirement that jetspeed should authenticate against a third party rest service which accepts username and password and returns back the User Object.

The most simplest and straightforward way I found of implementing this without effecting jetspeed too much was to write a custom AuthenticationProvider extending the DefaultAuthenticationProvider class and overriding the login method.

After I authenticate the user I get back the User details including roles, email, etc. Now if the user already exists in jetspeed database, I sync his roles, else I create the user and assign him the roles returned by the remote service.

Now I want a way to set the user.email, user.firstname and user.lastname properties too, so that it is accessible using $jetspeed.getUserAttribute in the psml files. Any idea how can we do this?

Here is my code [cut out unnecessary stuff] --

public class CustomAuthenticationProvider extends BaseAuthenticationProvider {

....

    public AuthenticatedUser authenticate(String userName, String pass开发者_高级运维word) throws SecurityException {


        try {

            //Login the user
            UserSessionDTO customSession = Security.login(userName, password);

            //Fetch the user details
            UserDTO customUser = customSession.getUser();

            //Get the user roles
            List<UserRoleDTO> roles = customUser.getUserRoleDTOList();

            //Verify/create the user in jetspeed user database
            UserImpl user = null;
            if (!um.userExists(customUser.getLoginId())) {
                user = (UserImpl) um.addUser(customUser.getLoginId(), true);

                //Standard data
                user.setMapped(true);
                user.setEnabled(true);
            } else {
                user = (UserImpl) um.getUser(customUser.getLoginId());
            }

            //Sync the portal user roles with the CMGI user roles
            List<Role> portalRoles = rm.getRolesForUser(customUser.getLoginId());
            for (Role portalRole : portalRoles) {
                Boolean found = Boolean.FALSE;
                for (UserRoleDTO role : roles) {
                    if (role.getRoleName().equalsIgnoreCase(portalRole.getName())) {
                        found = Boolean.TRUE;
                        break;
                    }
                }
                if(!found){
                    rm.removeRoleFromUser(userName, portalRole.getName());
                }
            }

            for(UserRoleDTO role : roles){
                rm.addRoleToUser(userName, role.getRoleName()); 
            }



            PasswordCredential pwc = new PasswordCredentialImpl(user, password);
            UserCredentialImpl uc = new UserCredentialImpl(pwc);
            AuthenticatedUserImpl authUser = new AuthenticatedUserImpl(user, uc);
            return authUser;


        } 

.... } }


You can add custom user attributes in Jetspeed user bean "org.apache.jetspeed.security.JetspeedPrincipalType.user" located in security-managers.xml.

These attributes should be defined like this e.g

        <bean class="org.apache.jetspeed.security.impl.SecurityAttributeTypeImpl">
          <constructor-arg index="0" value="user.lastname" />
          <constructor-arg index="1" value="info" />
        </bean>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜