Package org.apache.roller.weblogger.pojos

Examples of org.apache.roller.weblogger.pojos.User


     * Test basic persistence operations ... Create, Update, Delete.
     */
    public void testUserCRUD() throws Exception {
       
        UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
        User user = null;
       
        User testUser = new User();
        testUser.setUserName("testUser");
        testUser.setPassword("password");
        testUser.setScreenName("Test User Screen Name");
        testUser.setFullName("Test User");
        testUser.setEmailAddress("TestUser@dev.null");
        testUser.setLocale("en_US");
        testUser.setTimeZone("America/Los_Angeles");
        testUser.setDateCreated(new java.util.Date());
        testUser.setEnabled(Boolean.TRUE);
       
        // make sure test user does not exist
        user = mgr.getUserByUserName(testUser.getUserName());
        assertNull(user);
       
        // add test user
        mgr.addUser(testUser);
        String userName = testUser.getUserName();
        TestUtils.endSession(true);
       
        // make sure test user exists
        user = null;
        user = mgr.getUserByUserName(userName);
View Full Code Here


     * Test lookup mechanisms.
     */
    public void testUserLookups() throws Exception {
       
        UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
        User user = null;
       
        // add test user
        User testUser = TestUtils.setupUser("userTestUser");
        TestUtils.endSession(true);
       
        // lookup by username
        user = mgr.getUserByUserName(testUser.getUserName());
        assertNotNull(user);
        assertEquals(testUser.getUserName(), user.getUserName());
       
        // lookup by id
        String userName = user.getUserName();
        user = null;
        user = mgr.getUserByUserName(userName);
        assertNotNull(user);
        assertEquals(testUser.getUserName(), user.getUserName());
       
        // lookup by UserName (part)
        user = null;
        List users1 = mgr.getUsersStartingWith(testUser.getUserName().substring(0, 3), Boolean.TRUE, 0, 1);
        assertEquals(1, users1.size());
        user = (User) users1.get(0);
        assertNotNull(user);
        assertEquals(testUser.getUserName(), user.getUserName());
       
        // lookup by Email (part)
        user = null;
        List users2 = mgr.getUsersStartingWith(testUser.getEmailAddress().substring(0, 3), Boolean.TRUE, 0, 1);
        assertEquals(1, users2.size());
        user = (User) users2.get(0);
        assertNotNull(user);
        assertEquals(testUser.getUserName(), user.getUserName());
       
        // make sure disable users are not returned
        user.setEnabled(Boolean.FALSE);
        mgr.saveUser(user);
        TestUtils.endSession(true);
        user = null;
        user = mgr.getUserByUserName(testUser.getUserName());
        assertNull(user);
       
        // remove test user
        TestUtils.teardownUser(testUser.getUserName());
        TestUtils.endSession(true);
    }
View Full Code Here

     * Test basic user role persistence ... Add, Remove
     */
    public void testRoleCRUD() throws Exception {
       
        UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
        User user = null;
       
        // add test user
        User testUser = TestUtils.setupUser("roleTestUser");
        TestUtils.endSession(true);
       
        user = mgr.getUserByUserName(testUser.getUserName());
        assertNotNull(user);
       
        if (WebloggerConfig.getBooleanProperty("users.firstUserAdmin")) {
            assertEquals(2, mgr.getRoles(user).size());
            assertTrue(mgr.hasRole("editor", user));
            assertTrue(mgr.hasRole("admin", user));
        } else {
            assertEquals(1, mgr.getRoles(user).size());
            assertTrue(mgr.hasRole("editor", user));
            assertFalse(mgr.hasRole("admin", user));
        }
       
        // remove role
        mgr.revokeRole("editor",user);
        mgr.saveUser(user);
        TestUtils.endSession(true);
       
        // check that role was removed
        user = null;
        user = mgr.getUserByUserName(testUser.getUserName());
        assertNotNull(user);
        if (WebloggerConfig.getBooleanProperty("users.firstUserAdmin")) {
            assertEquals(1, mgr.getRoles(user).size());
            assertFalse(mgr.hasRole("editor", user));
            assertTrue(mgr.hasRole("admin", user));
        } else {
            assertEquals(0, mgr.getRoles(user).size());
            assertFalse(mgr.hasRole("editor", user));
            assertFalse(mgr.hasRole("admin", user));
        }
        // add role
        mgr.grantRole("editor", user);
        mgr.saveUser(user);
        TestUtils.endSession(true);
       
        // check that role was added
        user = null;
        user = mgr.getUserByUserName(testUser.getUserName());
        assertNotNull(user);
        if (WebloggerConfig.getBooleanProperty("users.firstUserAdmin")) {
            assertEquals(2, mgr.getRoles(user).size());
            assertTrue(mgr.hasRole("editor", user));
            assertTrue(mgr.hasRole("admin", user));
        } else {
            assertEquals(1, mgr.getRoles(user).size());
            assertTrue(mgr.hasRole("editor", user));
            assertFalse(mgr.hasRole("admin", user));
        }
        // remove test user
        TestUtils.teardownUser(testUser.getUserName());
        TestUtils.endSession(true);
    }
View Full Code Here

   
    public List getWeblogUsers(Weblog weblog, boolean enabledOnly) throws WebloggerException {
        List users = new ArrayList();
        List<WeblogPermission> perms = roller.getUserManager().getWeblogPermissions(weblog);
        for (WeblogPermission perm : perms) {
            User user = perm.getUser();
            if (user == null) {
                log.error("ERROR user is null, userName:" + perm.getUserName());
                continue;
            }
            if (!enabledOnly || user.getEnabled().booleanValue()) {
                users.add(user);
            }
        }
        return users;
    }
View Full Code Here

            return null;
        }
       
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
       
        User ud = new User();
        // setting default
        ud.setId(null);
        ud.setLocale(Locale.getDefault().toString());
        ud.setTimeZone(TimeZone.getDefault().getID());
        ud.setDateCreated(new java.util.Date());

        String userName = null;
        String password = null;
        String fullName = null;
        String email = null;
        String screenName = null;
        String locale = null;
        String timezone = null;
        boolean enabled = false;

        if(authentication == null) {
            // Try to get SSO data from HttpServletRequest
            userName = getRequestAttribute(request, WebloggerConfig.getProperty(UID_LDAP_PROPERTY, DEFAULT_UID_LDAP_ATTRIBUTE));

            screenName = getRequestAttribute(request, WebloggerConfig.getProperty(SNAME_LDAP_PROPERTY, DEFAULT_SNAME_LDAP_ATTRIBUTE));

            fullName = getRequestAttribute(request, WebloggerConfig.getProperty(NAME_LDAP_PROPERTY, DEFAULT_NAME_LDAP_ATTRIBUTE));

            email = getRequestAttribute(request, WebloggerConfig.getProperty(EMAIL_LDAP_PROPERTY, DEFAULT_EMAIL_LDAP_ATTRIBUTE));

            locale = getRequestAttribute(request, WebloggerConfig.getProperty(LOCALE_LDAP_PROPERTY, DEFAULT_LOCALE_LDAP_ATTRIBUTE));

            timezone = getRequestAttribute(request, WebloggerConfig.getProperty(TIMEZONE_LDAP_PROPERTY, DEFAULT_TIMEZONE_LDAP_ATTRIBUTE));

            if (userName == null && fullName == null && screenName == null &&
                    email == null && locale == null && timezone == null) {

                log.warn("No Authentication found in SecurityContextHolder and HttpServletRequest.");
                return null;
            } else {
                enabled = true;
            }
        } else {
       
            Object oPrincipal = authentication.getPrincipal();
       
            if(oPrincipal == null) {
                log.warn("Principal is null. Skipping auto-registration.");
                return null;
            }
       
            if (!(oPrincipal instanceof UserDetails)) {
                log.warn("Unsupported Principal type in Authentication. Skipping auto-registration.");
                return null;
            }
       
            UserDetails userDetails = (UserDetails) oPrincipal;
       
            userName = userDetails.getUsername();
            password = userDetails.getPassword();
            enabled = userDetails.isEnabled();
       
       
            if(userDetails instanceof RollerUserDetails) {
                RollerUserDetails rollerDetails = (RollerUserDetails) userDetails;

                screenName = rollerDetails.getScreenName();
                fullName = rollerDetails.getFullName();
                email = rollerDetails.getEmailAddress();
                locale = rollerDetails.getLocale();
                timezone = rollerDetails.getTimeZone();
           
            } else if(userDetails instanceof LdapUserDetails) {
                LdapUserDetails ldapDetails = (LdapUserDetails) userDetails;

                Attributes attributes = ldapDetails.getAttributes();
                screenName = getLdapAttribute(attributes, WebloggerConfig.getProperty(SNAME_LDAP_PROPERTY, DEFAULT_SNAME_LDAP_ATTRIBUTE));
                fullName = getLdapAttribute(attributes, WebloggerConfig.getProperty(NAME_LDAP_PROPERTY, DEFAULT_NAME_LDAP_ATTRIBUTE));
                email = getLdapAttribute(attributes, WebloggerConfig.getProperty(EMAIL_LDAP_PROPERTY, DEFAULT_EMAIL_LDAP_ATTRIBUTE));
                locale = getLdapAttribute(attributes, WebloggerConfig.getProperty(LOCALE_LDAP_PROPERTY, DEFAULT_LOCALE_LDAP_ATTRIBUTE));
                timezone = getLdapAttribute(attributes, WebloggerConfig.getProperty(TIMEZONE_LDAP_PROPERTY, DEFAULT_TIMEZONE_LDAP_ATTRIBUTE));
           
            }
        }

        boolean storePassword = WebloggerConfig.getBooleanProperty("users.sso.passwords.save");
        if(!storePassword) {
            password = WebloggerConfig.getProperty("users.sso.passwords.defaultValue","<unknown>");
        }

        ud.setPassword(password);
        ud.setEnabled(enabled ? Boolean.TRUE : Boolean.FALSE);

        ud.setUserName(userName);
        ud.setFullName(fullName);
        ud.setEmailAddress(email);
        ud.setScreenName(screenName);
        if (locale != null) {
            ud.setLocale(locale);
        }
        if (timezone != null) {
            ud.setTimeZone(timezone);
        }

        return ud;
    }
View Full Code Here

        try {

            boolean usingSSO = WebloggerConfig.getBooleanProperty("users.sso.enabled");
            if (usingSSO) {
                // See if user is already logged in via Acegi
                User fromSSO = CustomUserRegistry.getUserDetailsFromAuthentication(getServletRequest());
                if (fromSSO != null) {
                    // Copy user details from Acegi, including LDAP attributes
                    getBean().copyFrom(fromSSO);
                    setFromSso(true);
                }
View Full Code Here

        if (!hasActionErrors()) try {
           
            UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
           
            // copy form data into new user pojo
            User ud = new User();
            getBean().copyTo(ud); // doesn't copy password
            ud.setUserName(getBean().getUserName());
            ud.setDateCreated(new java.util.Date());
            ud.setEnabled(Boolean.TRUE);
           
            // If user set both password and passwordConfirm then reset password
            if (!StringUtils.isEmpty(getBean().getPasswordText()) &&
                    !StringUtils.isEmpty(getBean().getPasswordConfirm())) {
                ud.resetPassword(getBean().getPasswordText());
            }
           
            // are we using email activation?
            boolean activationEnabled = WebloggerRuntimeConfig.getBooleanProperty(
                    "user.account.activation.enabled");
            if (activationEnabled) {
                // User account will be enabled after the activation process
                ud.setEnabled(Boolean.FALSE);
               
                // Create & save the activation data
                String activationCode = UUID.randomUUID().toString();
               
                if (mgr.getUserByActivationCode(activationCode) != null) {
                    // In the *extremely* unlikely event that we generate an
                    // activation code that is already use, we'll retry 3 times.
                    int numOfRetries = 3;
                    if (numOfRetries < 1) numOfRetries = 1;
                    for (int i = 0; i < numOfRetries; i++) {
                        activationCode = UUID.randomUUID().toString();
                        if (mgr.getUserByActivationCode(activationCode) == null) {
                            break;
                        } else {
                            activationCode = null;
                        }
                    }
                    // In more unlikely event that three retries isn't enough
                    if (activationCode == null){
                        throw new WebloggerException("error.add.user.activationCodeInUse");
                    }
                }
                ud.setActivationCode(activationCode);
            }
           
            // save new user
            mgr.addUser(ud);
           
            String openidurl = getBean().getOpenIdUrl();
            if (openidurl != null) {
                if (openidurl.endsWith("/")) {
                    openidurl = openidurl.substring(0, openidurl.length() - 1);
                }
                mgr.setUserAttribute(
                    ud.getUserName(), UserAttribute.Attributes.OPENID_URL.toString(),
                    openidurl);
            }
           
            WebloggerFactory.getWeblogger().flush();
           
            // now send activation email if necessary
            if (activationEnabled && ud.getActivationCode() != null) {
                try {
                    // send activation mail to the user
                    MailUtil.sendUserActivationEmail(ud);
                } catch (WebloggerException ex) {
                    log.error("Error sending activation email to - "+ud.getEmailAddress(), ex);
                }
               
                setActivationStatus("pending");
            }
            
View Full Code Here

            UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
           
            if (getActivationCode() == null) {
                addError("error.activate.user.missingActivationCode");
            } else {
                User user = mgr.getUserByActivationCode(getActivationCode());
               
                if (user != null) {
                    // enable user account
                    user.setEnabled(Boolean.TRUE);
                    user.setActivationCode(null);
                    mgr.saveUser(user);
                    WebloggerFactory.getWeblogger().flush();
                   
                    setActivationStatus("active");
                   
View Full Code Here

        if (usingSSO) {
            boolean storePassword = WebloggerConfig.getBooleanProperty("users.sso.passwords.saveInRollerDb");
            String password = WebloggerConfig.getProperty("users.sso.passwords.defaultValue", "<unknown>");
           
            // Preserve username and password, Acegi case            
            User fromSSO = CustomUserRegistry.getUserDetailsFromAuthentication(getServletRequest());
            if (fromSSO != null) {
                if (storePassword) {
                    password = fromSSO.getPassword();
                }
                getBean().setPasswordText(password);
                getBean().setPasswordConfirm(password);
                getBean().setUserName(fromSSO.getUserName());
                setFromSso(true);
            }

            // Preserve username and password, CMA case            
            else if (getServletRequest().getUserPrincipal() != null) {
View Full Code Here

            // Thowing a "soft" exception here allows setup to procede
            throw new UsernameNotFoundException("User info not available yet.");
        }
        try {
            UserManager umgr = roller.getUserManager();
            User userData = null
            if (userName.startsWith("http://")) {
                if (userName.endsWith("/")) {
                    userName = userName.substring(0, userName.length() -1 );
                }
                try {
                    userData = umgr.getUserByAttribute(
                        UserAttribute.Attributes.OPENID_URL.toString(),
                        userName);
                } catch (WebloggerException ex) {
                    throw new DataRetrievalFailureException("ERROR in user lookup", ex);
                }
                String name;
                String password;
                GrantedAuthority[] authorities;
               
                // We are not throwing UsernameNotFound exception in case of
                // openid authentication in order to recieve user SREG attributes
                // from the authentication filter and save them               
                if (userData == null) {
                     authorities = new GrantedAuthority[1];
                     GrantedAuthority g = new GrantedAuthorityImpl("openidLogin");
                     authorities[0] = g;
                     name = "openid";
                     password = "openid";
                } else {
                     authorities =  getAuthorities(userData, umgr);
                     name = userData.getUserName();
                     password = userData.getPassword();
                }
                UserDetails usr = new org.springframework.security.userdetails.User(name, password, true, authorities);
                return  usr;
               
            } else {
                try {
                    userData = umgr.getUserByUserName(userName);
                } catch (WebloggerException ex) {
                    throw new DataRetrievalFailureException("ERROR in user lookup", ex);
                }
                if (userData == null) {
                    throw new UsernameNotFoundException("ERROR no user: " + userName);
                }
                GrantedAuthority[] authorities =  getAuthorities(userData, umgr);       
                return new org.springframework.security.userdetails.User(userData.getUserName(), userData.getPassword(), true, authorities);
            }           
        } catch (WebloggerException ex) {
            throw new DataAccessResourceFailureException("ERROR: fetching roles", ex);
        }
       
View Full Code Here

TOP

Related Classes of org.apache.roller.weblogger.pojos.User

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.