Search in sources :

Example 1 with UserCreator

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

the class UserServiceTest method testCreate.

/**
 * We should ignore this test until we have build fixed.
 */
@Test
public void testCreate() throws Exception {
    // prepare the UserCreator
    long now = (new Date()).getTime();
    String username = MessageFormat.format("aaa_test_username_{0,number,#}", now);
    String userEmail = MessageFormat.format("testuser_{0,number,#}@organization.com", now);
    String displayName = MessageFormat.format("User Display Name {0}", now);
    // KapuaPeid accountPeid = KapuaEidGenerator.generate();//
    KapuaLocator locator = KapuaLocator.getInstance();
    IdGeneratorService idGeneratorService = locator.getService(IdGeneratorService.class);
    KapuaId scopeId = idGeneratorService.generate();
    KapuaLocator serviceLocator = KapuaLocator.getInstance();
    UserFactory kapuaEntityCreatorFactory = serviceLocator.getFactory(UserFactory.class);
    UserCreator userCreator = kapuaEntityCreatorFactory.newCreator(scopeId, username);
    userCreator.setDisplayName(displayName);
    userCreator.setEmail(userEmail);
    userCreator.setPhoneNumber("+1 555 123 4567");
    // create the User
    UserService userService = serviceLocator.getService(UserService.class);
    User user = userService.create(userCreator);
    user = userService.find(user.getScopeId(), user.getId());
    // 
    // User asserts
    assertNotNull(user.getId());
    assertNotNull(user.getId().getId());
    assertTrue(user.getOptlock() >= 0);
    assertEquals(scopeId, user.getScopeId());
    assertEquals(userCreator.getName(), user.getName());
    assertNotNull(user.getCreatedOn());
    assertNotNull(user.getCreatedBy());
    assertNotNull(user.getModifiedOn());
    assertNotNull(user.getModifiedBy());
    assertEquals(userCreator.getDisplayName(), user.getDisplayName());
    assertEquals(userCreator.getEmail(), user.getEmail());
    assertEquals(userCreator.getPhoneNumber(), user.getPhoneNumber());
    assertEquals(UserStatus.ENABLED, user.getStatus());
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) User(org.eclipse.kapua.service.user.User) UserService(org.eclipse.kapua.service.user.UserService) UserFactory(org.eclipse.kapua.service.user.UserFactory) KapuaId(org.eclipse.kapua.model.id.KapuaId) UserCreator(org.eclipse.kapua.service.user.UserCreator) IdGeneratorService(org.eclipse.kapua.service.generator.id.IdGeneratorService) Date(java.util.Date) Test(org.junit.Test)

Example 2 with UserCreator

use of org.eclipse.kapua.service.user.UserCreator 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)

Example 3 with UserCreator

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

the class DeviceCommandManagementServiceTest method setUpTest.

@Ignore
@Before
public void setUpTest() throws Exception {
    // 
    // User creation
    String userName = String.format("test-usr-%d", (new Date()).getTime());
    UserService userService = locator.getService(UserService.class);
    UserFactory userFactory = locator.getFactory(UserFactory.class);
    UserCreator userCreator = userFactory.newCreator(account.getId(), userName);
    user = userService.create(userCreator);
    // 
    // User permission creation
    UserPermissionService userPermissionService = locator.getService(UserPermissionService.class);
    UserPermissionFactory userPermissionFactory = locator.getFactory(UserPermissionFactory.class);
    UserPermissionCreator userPermissionCreator = userPermissionFactory.newCreator(account.getId());
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    userPermissionCreator.setUserId(user.getId());
    userPermissionCreator.setPermission(permissionFactory.newPermission(DeviceLifecycleDomain.DEVICE_LIFECYCLE, Actions.connect, account.getId()));
    userPermissionService.create(userPermissionCreator);
    // 
    // User credentials creation
    CredentialService credentialService = locator.getService(CredentialService.class);
    CredentialFactory credentialFactory = locator.getFactory(CredentialFactory.class);
    CredentialCreator credentialCreator = credentialFactory.newCreator(account.getId(), user.getId(), CredentialType.PASSWORD, "kapua-password");
    credentialService.create(credentialCreator);
}
Also used : CredentialService(org.eclipse.kapua.service.authentication.credential.CredentialService) UserService(org.eclipse.kapua.service.user.UserService) 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) UserPermissionService(org.eclipse.kapua.service.authorization.user.permission.UserPermissionService) UserPermissionFactory(org.eclipse.kapua.service.authorization.user.permission.UserPermissionFactory) UserCreator(org.eclipse.kapua.service.user.UserCreator) CredentialCreator(org.eclipse.kapua.service.authentication.credential.CredentialCreator) Date(java.util.Date) UserPermissionCreator(org.eclipse.kapua.service.authorization.user.permission.UserPermissionCreator) Before(org.junit.Before) Ignore(org.junit.Ignore)

Aggregations

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