Search in sources :

Example 6 with UserService

use of org.eclipse.kapua.service.user.UserService in project kapua by eclipse.

the class GwtAuthorizationServiceImpl method getCurrentSession.

/**
 * Return the currently authenticated user or null if no session has been established.
 */
public GwtSession getCurrentSession() throws GwtKapuaException {
    GwtSession gwtSession = null;
    try {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser != null && currentUser.isAuthenticated()) {
            Session session = currentUser.getSession();
            gwtSession = (GwtSession) session.getAttribute(SESSION_CURRENT);
            // Store the user information in the sessions
            String username = (String) currentUser.getPrincipal();
            KapuaLocator locator = KapuaLocator.getInstance();
            UserService userService = locator.getService(UserService.class);
            User user = userService.findByName(username);
            // get the session
            if (gwtSession == null) {
                gwtSession = establishSession();
            } else {
                gwtSession.setGwtUser(KapuaGwtConverter.convert(user));
            }
        }
    } catch (Throwable t) {
        s_logger.warn("Error in getCurrentSession.", t);
        KapuaExceptionHandler.handle(t);
    }
    return gwtSession;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) User(org.eclipse.kapua.service.user.User) UserService(org.eclipse.kapua.service.user.UserService) GwtSession(org.eclipse.kapua.app.console.shared.model.GwtSession) Subject(org.apache.shiro.subject.Subject) Session(org.apache.shiro.session.Session) GwtSession(org.eclipse.kapua.app.console.shared.model.GwtSession) KapuaSession(org.eclipse.kapua.commons.security.KapuaSession)

Example 7 with UserService

use of org.eclipse.kapua.service.user.UserService in project kapua by eclipse.

the class GwtUserServiceImpl method findAll.

public ListLoadResult<GwtUser> findAll(String scopeIdString) throws GwtKapuaException {
    KapuaId scopeId = KapuaEid.parseShortId(scopeIdString);
    List<GwtUser> gwtUserList = new ArrayList<GwtUser>();
    try {
        KapuaLocator locator = KapuaLocator.getInstance();
        UserService userService = locator.getService(UserService.class);
        UserFactory userFactory = locator.getFactory(UserFactory.class);
        UserQuery query = userFactory.newQuery(scopeId);
        UserListResult list = userService.query(query);
        for (User user : list.getItems()) {
            gwtUserList.add(KapuaGwtConverter.convert(user));
        }
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return new BaseListLoadResult<GwtUser>(gwtUserList);
}
Also used : BaseListLoadResult(com.extjs.gxt.ui.client.data.BaseListLoadResult) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) User(org.eclipse.kapua.service.user.User) GwtUserService(org.eclipse.kapua.app.console.shared.service.GwtUserService) UserService(org.eclipse.kapua.service.user.UserService) UserQuery(org.eclipse.kapua.service.user.UserQuery) ArrayList(java.util.ArrayList) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) UserFactory(org.eclipse.kapua.service.user.UserFactory) KapuaId(org.eclipse.kapua.model.id.KapuaId) UserListResult(org.eclipse.kapua.service.user.UserListResult)

Example 8 with UserService

use of org.eclipse.kapua.service.user.UserService in project kapua by eclipse.

the class GwtUserServiceImpl method update.

public GwtUser update(GwtXSRFToken xsrfToken, GwtUser gwtUser) throws GwtKapuaException {
    checkXSRFToken(xsrfToken);
    GwtUser gwtUserUpdated = null;
    try {
        KapuaLocator locator = KapuaLocator.getInstance();
        UserService userService = locator.getService(UserService.class);
        KapuaId scopeId = KapuaEid.parseShortId(gwtUser.getScopeId());
        KapuaId userId = KapuaEid.parseShortId(gwtUser.getId());
        User user = userService.find(scopeId, userId);
        if (user != null) {
            // 
            // Update user
            user.setName(gwtUser.getUnescapedUsername());
            user.setDisplayName(gwtUser.getUnescapedDisplayName());
            user.setEmail(gwtUser.getUnescapedEmail());
            user.setPhoneNumber(gwtUser.getUnescapedPhoneNumber());
            // status
            user.setStatus(UserStatus.valueOf(gwtUser.getStatus()));
            // 
            // Update permissions
            Set<String> newPermissions = new HashSet<String>();
            if (gwtUser.getPermissions() != null) {
                // build the set of permissions
                newPermissions.addAll(Arrays.asList(gwtUser.getPermissions().split(",")));
            }
            UserPermissionService userPermissionService = locator.getService(UserPermissionService.class);
            UserPermissionFactory userPermissionFactory = locator.getFactory(UserPermissionFactory.class);
            PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
            Set<UserPermissionCreator> newUserPermissions = new HashSet<UserPermissionCreator>();
            for (String p : newPermissions) {
                UserPermissionCreator userPermissionCreator = userPermissionFactory.newCreator(user.getScopeId());
                userPermissionCreator.setUserId(scopeId);
                String[] tokens = p.split(":");
                String domain = null;
                Actions action = null;
                KapuaId targetScopeId = null;
                if (tokens.length > 0) {
                    domain = tokens[0];
                }
                if (tokens.length > 1) {
                    action = Actions.valueOf(tokens[1]);
                }
                if (tokens.length > 2) {
                    targetScopeId = KapuaEid.parseShortId(tokens[2]);
                }
                Permission permission = permissionFactory.newPermission(domain, action, targetScopeId);
                userPermissionCreator.setPermission(permission);
                userPermissionService.create(userPermissionCreator);
            }
            userPermissionService.merge(newUserPermissions);
            // Update credentials
            if (gwtUser.getPassword() != null) {
                CredentialService credentialService = locator.getService(CredentialService.class);
                CredentialFactory credentialFactory = locator.getFactory(CredentialFactory.class);
                CredentialListResult credentials = credentialService.findByUserId(scopeId, userId);
                if (!credentials.isEmpty()) {
                    // 
                    // Delete old PASSWORD credential
                    Credential oldCredential = null;
                    for (Credential c : credentials.getItems()) {
                        if (CredentialType.PASSWORD.equals(c.getCredentialType())) {
                            oldCredential = c;
                            break;
                        }
                    }
                    credentialService.delete(oldCredential.getScopeId(), oldCredential.getId());
                    // 
                    // Create new PASSWORD credential
                    CredentialCreator credentialCreator = credentialFactory.newCreator(scopeId, user.getId(), CredentialType.PASSWORD, gwtUser.getPassword());
                    credentialService.create(credentialCreator);
                }
            }
            // optlock
            user.setOptlock(gwtUser.getOptlock());
            // update the user
            userService.update(user);
            // 
            // convert to GwtAccount and return
            // reload the user as we want to load all its permissions
            gwtUserUpdated = KapuaGwtConverter.convert(userService.find(user.getScopeId(), user.getId()));
        }
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return gwtUserUpdated;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Credential(org.eclipse.kapua.service.authentication.credential.Credential) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) User(org.eclipse.kapua.service.user.User) GwtUserService(org.eclipse.kapua.app.console.shared.service.GwtUserService) UserService(org.eclipse.kapua.service.user.UserService) Actions(org.eclipse.kapua.service.authorization.permission.Actions) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) UserPermissionFactory(org.eclipse.kapua.service.authorization.user.permission.UserPermissionFactory) CredentialFactory(org.eclipse.kapua.service.authentication.credential.CredentialFactory) CredentialCreator(org.eclipse.kapua.service.authentication.credential.CredentialCreator) CredentialService(org.eclipse.kapua.service.authentication.credential.CredentialService) Permission(org.eclipse.kapua.service.authorization.permission.Permission) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) UserPermissionService(org.eclipse.kapua.service.authorization.user.permission.UserPermissionService) UserPermissionFactory(org.eclipse.kapua.service.authorization.user.permission.UserPermissionFactory) CredentialListResult(org.eclipse.kapua.service.authentication.credential.CredentialListResult) KapuaId(org.eclipse.kapua.model.id.KapuaId) HashSet(java.util.HashSet) UserPermissionCreator(org.eclipse.kapua.service.authorization.user.permission.UserPermissionCreator)

Example 9 with UserService

use of org.eclipse.kapua.service.user.UserService in project kapua by eclipse.

the class GwtUserServiceImpl method delete.

public void delete(GwtXSRFToken xsrfToken, String accountId, GwtUser gwtUser) throws GwtKapuaException {
    checkXSRFToken(xsrfToken);
    KapuaId scopeId = KapuaEid.parseShortId(accountId);
    try {
        KapuaLocator locator = KapuaLocator.getInstance();
        UserService userService = locator.getService(UserService.class);
        User user = userService.find(scopeId, KapuaEid.parseShortId(gwtUser.getId()));
        if (user != null) {
            userService.delete(user);
        }
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) User(org.eclipse.kapua.service.user.User) GwtUserService(org.eclipse.kapua.app.console.shared.service.GwtUserService) UserService(org.eclipse.kapua.service.user.UserService) KapuaId(org.eclipse.kapua.model.id.KapuaId)

Example 10 with UserService

use of org.eclipse.kapua.service.user.UserService in project kapua by eclipse.

the class GwtUserServiceImpl method create.

public GwtUser create(GwtXSRFToken xsrfToken, GwtUserCreator gwtUserCreator) throws GwtKapuaException {
    checkXSRFToken(xsrfToken);
    GwtUser gwtUser = null;
    try {
        KapuaLocator locator = KapuaLocator.getInstance();
        UserFactory userFactory = locator.getFactory(UserFactory.class);
        KapuaId scopeId = KapuaEid.parseShortId(gwtUserCreator.getScopeId());
        UserCreator userCreator = userFactory.newCreator(scopeId, gwtUserCreator.getUsername());
        userCreator.setDisplayName(gwtUserCreator.getDisplayName());
        userCreator.setEmail(gwtUserCreator.getEmail());
        userCreator.setPhoneNumber(gwtUserCreator.getPhoneNumber());
        // 
        // Create the User
        UserService userService = locator.getService(UserService.class);
        User user = userService.create(userCreator);
        // 
        // Create permissions
        Set<String> permissions = new HashSet<String>();
        if (gwtUserCreator.getPermissions() != null) {
            // build the set of permissions
            permissions.addAll(Arrays.asList(gwtUserCreator.getPermissions().split(",")));
        }
        UserPermissionService userPermissionService = locator.getService(UserPermissionService.class);
        UserPermissionFactory userPermissionFactory = locator.getFactory(UserPermissionFactory.class);
        PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
        for (String p : permissions) {
            UserPermissionCreator userPermissionCreator = userPermissionFactory.newCreator(user.getScopeId());
            userPermissionCreator.setUserId(scopeId);
            String[] tokens = p.split(":");
            String domain = null;
            Actions action = null;
            KapuaId targetScopeId = null;
            if (tokens.length > 0) {
                domain = tokens[0];
            }
            if (tokens.length > 1) {
                action = Actions.valueOf(tokens[1]);
            }
            if (tokens.length > 2) {
                targetScopeId = KapuaEid.parseShortId(tokens[2]);
            }
            Permission permission = permissionFactory.newPermission(domain, action, targetScopeId);
            userPermissionCreator.setPermission(permission);
            userPermissionService.create(userPermissionCreator);
        }
        // 
        // Create credentials
        CredentialService credentialService = locator.getService(CredentialService.class);
        CredentialFactory credentialFactory = locator.getFactory(CredentialFactory.class);
        CredentialCreator credentialCreator = credentialFactory.newCreator(scopeId, user.getId(), CredentialType.PASSWORD, gwtUserCreator.getPassword());
        credentialService.create(credentialCreator);
        // convert to GwtAccount and return
        // reload the user as we want to load all its permissions
        gwtUser = KapuaGwtConverter.convert(userService.find(user.getScopeId(), user.getId()));
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return gwtUser;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) User(org.eclipse.kapua.service.user.User) GwtUserService(org.eclipse.kapua.app.console.shared.service.GwtUserService) UserService(org.eclipse.kapua.service.user.UserService) Actions(org.eclipse.kapua.service.authorization.permission.Actions) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) UserPermissionFactory(org.eclipse.kapua.service.authorization.user.permission.UserPermissionFactory) CredentialFactory(org.eclipse.kapua.service.authentication.credential.CredentialFactory) UserFactory(org.eclipse.kapua.service.user.UserFactory) CredentialCreator(org.eclipse.kapua.service.authentication.credential.CredentialCreator) CredentialService(org.eclipse.kapua.service.authentication.credential.CredentialService) Permission(org.eclipse.kapua.service.authorization.permission.Permission) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) UserPermissionService(org.eclipse.kapua.service.authorization.user.permission.UserPermissionService) UserPermissionFactory(org.eclipse.kapua.service.authorization.user.permission.UserPermissionFactory) KapuaId(org.eclipse.kapua.model.id.KapuaId) UserCreator(org.eclipse.kapua.service.user.UserCreator) GwtUserCreator(org.eclipse.kapua.app.console.shared.model.GwtUserCreator) HashSet(java.util.HashSet) UserPermissionCreator(org.eclipse.kapua.service.authorization.user.permission.UserPermissionCreator)

Aggregations

UserService (org.eclipse.kapua.service.user.UserService)12 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)11 User (org.eclipse.kapua.service.user.User)11 GwtUser (org.eclipse.kapua.app.console.shared.model.GwtUser)7 KapuaId (org.eclipse.kapua.model.id.KapuaId)6 GwtUserService (org.eclipse.kapua.app.console.shared.service.GwtUserService)5 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)5 CredentialService (org.eclipse.kapua.service.authentication.credential.CredentialService)4 UserPermissionFactory (org.eclipse.kapua.service.authorization.user.permission.UserPermissionFactory)4 UserPermissionService (org.eclipse.kapua.service.authorization.user.permission.UserPermissionService)4 UserFactory (org.eclipse.kapua.service.user.UserFactory)4 KapuaSession (org.eclipse.kapua.commons.security.KapuaSession)3 CredentialCreator (org.eclipse.kapua.service.authentication.credential.CredentialCreator)3 CredentialFactory (org.eclipse.kapua.service.authentication.credential.CredentialFactory)3 Permission (org.eclipse.kapua.service.authorization.permission.Permission)3 Date (java.util.Date)2 HashSet (java.util.HashSet)2 Callable (java.util.concurrent.Callable)2 ShiroException (org.apache.shiro.ShiroException)2 AuthenticationException (org.apache.shiro.authc.AuthenticationException)2