Search in sources :

Example 1 with GwtUserCreator

use of org.eclipse.kapua.app.console.shared.model.GwtUserCreator in project kapua by eclipse.

the class UserManageForm method submitAccount.

protected void submitAccount() {
    // New account
    if (m_existingUser == null) {
        final GwtUserCreator gwtUserCreator = new GwtUserCreator();
        gwtUserCreator.setScopeId(m_accountId);
        gwtUserCreator.setUsername(username.getValue());
        gwtUserCreator.setPassword(password.getValue());
        gwtUserCreator.setDisplayName(displayName.getValue());
        gwtUserCreator.setEmail(email.getValue());
        gwtUserCreator.setPhoneNumber(phoneNumber.getValue());
        gwtUserCreator.setPermissions(getEnabledUserPermissions());
        gwtXSRFService.generateSecurityToken(new AsyncCallback<GwtXSRFToken>() {

            @Override
            public void onFailure(Throwable ex) {
                FailureHandler.handle(ex);
            }

            @Override
            public void onSuccess(GwtXSRFToken token) {
                gwtUserService.create(token, gwtUserCreator, new AsyncCallback<GwtUser>() {

                    public void onFailure(Throwable caught) {
                        FailureHandler.handleFormException(m_formPanel, caught);
                        m_status.hide();
                        m_formPanel.getButtonBar().enable();
                    }

                    public void onSuccess(GwtUser user) {
                        ConsoleInfo.display(MSGS.info(), MSGS.userCreatedConfirmation(user.getUnescapedUsername()));
                        hide();
                    }
                });
            }
        });
    } else {
        m_existingUser.setAdministrator(false);
        m_existingUser.setPermissions(getEnabledUserPermissions());
        super.submitAccount();
    }
}
Also used : AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) GwtUserCreator(org.eclipse.kapua.app.console.shared.model.GwtUserCreator) GwtXSRFToken(org.eclipse.kapua.app.console.shared.model.GwtXSRFToken)

Example 2 with GwtUserCreator

use of org.eclipse.kapua.app.console.shared.model.GwtUserCreator 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

GwtUser (org.eclipse.kapua.app.console.shared.model.GwtUser)2 GwtUserCreator (org.eclipse.kapua.app.console.shared.model.GwtUserCreator)2 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1 HashSet (java.util.HashSet)1 GwtXSRFToken (org.eclipse.kapua.app.console.shared.model.GwtXSRFToken)1 GwtUserService (org.eclipse.kapua.app.console.shared.service.GwtUserService)1 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)1 KapuaId (org.eclipse.kapua.model.id.KapuaId)1 CredentialCreator (org.eclipse.kapua.service.authentication.credential.CredentialCreator)1 CredentialFactory (org.eclipse.kapua.service.authentication.credential.CredentialFactory)1 CredentialService (org.eclipse.kapua.service.authentication.credential.CredentialService)1 Actions (org.eclipse.kapua.service.authorization.permission.Actions)1 Permission (org.eclipse.kapua.service.authorization.permission.Permission)1 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)1 UserPermissionCreator (org.eclipse.kapua.service.authorization.user.permission.UserPermissionCreator)1 UserPermissionFactory (org.eclipse.kapua.service.authorization.user.permission.UserPermissionFactory)1 UserPermissionService (org.eclipse.kapua.service.authorization.user.permission.UserPermissionService)1 User (org.eclipse.kapua.service.user.User)1 UserCreator (org.eclipse.kapua.service.user.UserCreator)1 UserFactory (org.eclipse.kapua.service.user.UserFactory)1