Search in sources :

Example 16 with UserImpl

use of org.graylog2.users.UserImpl in project graylog2-server by Graylog2.

the class UserServiceImplTest method testGetPermissionsForUser.

@Test
public void testGetPermissionsForUser() throws Exception {
    final InMemoryRolePermissionResolver permissionResolver = mock(InMemoryRolePermissionResolver.class);
    final GRNRegistry grnRegistry = GRNRegistry.createWithBuiltinTypes();
    final UserService userService = new UserServiceImpl(mongoConnection, configuration, roleService, accessTokenService, userFactory, permissionResolver, serverEventBus, grnRegistry, permissionAndRoleResolver);
    final UserImplFactory factory = new UserImplFactory(new Configuration(), permissions);
    final UserImpl user = factory.create(new HashMap<>());
    user.setName("user");
    final Role role = createRole("Foo");
    user.setRoleIds(Collections.singleton(role.getId()));
    user.setPermissions(Collections.singletonList("hello:world"));
    when(permissionResolver.resolveStringPermission(role.getId())).thenReturn(Collections.singleton("foo:bar"));
    final GRNPermission ownerShipPermission = GRNPermission.create(RestPermissions.ENTITY_OWN, grnRegistry.newGRN(GRNTypes.DASHBOARD, "1234"));
    final GRN userGRN = grnRegistry.ofUser(user);
    when(permissionAndRoleResolver.resolvePermissionsForPrincipal(userGRN)).thenReturn(ImmutableSet.of(new CaseSensitiveWildcardPermission("perm:from:grant"), ownerShipPermission));
    final String roleId = "12345";
    when(permissionAndRoleResolver.resolveRolesForPrincipal(userGRN)).thenReturn(ImmutableSet.of(roleId));
    when(permissionResolver.resolveStringPermission(roleId)).thenReturn(ImmutableSet.of("perm:from:role"));
    assertThat(userService.getPermissionsForUser(user).stream().map(p -> p instanceof CaseSensitiveWildcardPermission ? p.toString() : p).collect(Collectors.toSet())).containsExactlyInAnyOrder("users:passwordchange:user", "users:edit:user", "foo:bar", "hello:world", "users:tokenlist:user", "users:tokencreate:user", "users:tokenremove:user", "perm:from:grant", ownerShipPermission, "perm:from:role");
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) InMemoryRolePermissionResolver(org.graylog2.security.InMemoryRolePermissionResolver) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) GRNRegistry(org.graylog.grn.GRNRegistry) EventBus(com.google.common.eventbus.EventBus) DBObject(com.mongodb.DBObject) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AccessTokenService(org.graylog2.security.AccessTokenService) MongoDBInstance(org.graylog.testing.mongodb.MongoDBInstance) Map(java.util.Map) MockitoJUnit(org.mockito.junit.MockitoJUnit) Before(org.junit.Before) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) GRNTypes(org.graylog.grn.GRNTypes) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) GRN(org.graylog.grn.GRN) List(java.util.List) Rule(org.junit.Rule) Configuration(org.graylog2.Configuration) UserService(org.graylog2.shared.users.UserService) RestPermissions(org.graylog2.shared.security.RestPermissions) ObjectId(org.bson.types.ObjectId) PasswordAlgorithm(org.graylog2.plugin.security.PasswordAlgorithm) Optional(java.util.Optional) SHA1HashPasswordAlgorithm(org.graylog2.security.hashing.SHA1HashPasswordAlgorithm) MockitoRule(org.mockito.junit.MockitoRule) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) MongoConnection(org.graylog2.database.MongoConnection) User(org.graylog2.plugin.database.users.User) Role(org.graylog2.shared.users.Role) GRNPermission(org.graylog.security.permissions.GRNPermission) Permissions(org.graylog2.shared.security.Permissions) Collections(java.util.Collections) PermissionAndRoleResolver(org.graylog.security.PermissionAndRoleResolver) CaseSensitiveWildcardPermission(org.graylog.security.permissions.CaseSensitiveWildcardPermission) Mockito.mock(org.mockito.Mockito.mock) GRN(org.graylog.grn.GRN) GRNRegistry(org.graylog.grn.GRNRegistry) Configuration(org.graylog2.Configuration) UserService(org.graylog2.shared.users.UserService) InMemoryRolePermissionResolver(org.graylog2.security.InMemoryRolePermissionResolver) CaseSensitiveWildcardPermission(org.graylog.security.permissions.CaseSensitiveWildcardPermission) Role(org.graylog2.shared.users.Role) GRNPermission(org.graylog.security.permissions.GRNPermission) Test(org.junit.Test)

Example 17 with UserImpl

use of org.graylog2.users.UserImpl in project graylog2-server by Graylog2.

the class UserImplTest method testLastNameLengthValidation.

@Test
public void testLastNameLengthValidation() {
    user = new UserImpl(null, null, null);
    ValidationResult result = user.getValidations().get(UserImpl.LAST_NAME).validate(StringUtils.repeat("*", 10));
    assertTrue(result.passed());
    result = user.getValidations().get(UserImpl.LAST_NAME).validate(StringUtils.repeat("*", 210));
    assertFalse(result.passed());
}
Also used : ValidationResult(org.graylog2.plugin.database.validators.ValidationResult) Test(org.junit.Test)

Example 18 with UserImpl

use of org.graylog2.users.UserImpl in project graylog2-server by Graylog2.

the class UserImplTest method testFirstNameLengthValidation.

@Test
public void testFirstNameLengthValidation() {
    user = new UserImpl(null, null, null);
    ValidationResult result = user.getValidations().get(UserImpl.FIRST_NAME).validate(StringUtils.repeat("*", 10));
    assertTrue(result.passed());
    result = user.getValidations().get(UserImpl.FIRST_NAME).validate(StringUtils.repeat("*", 210));
    assertFalse(result.passed());
}
Also used : ValidationResult(org.graylog2.plugin.database.validators.ValidationResult) Test(org.junit.Test)

Example 19 with UserImpl

use of org.graylog2.users.UserImpl in project graylog2-server by Graylog2.

the class UserContextTest method runAs.

@Test
void runAs() {
    // Simulate what we do in the DefaultSecurityManagerProvider
    DefaultSecurityManager sm = new DefaultSecurityManager();
    SecurityUtils.setSecurityManager(sm);
    final DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
    final DefaultSessionStorageEvaluator sessionStorageEvaluator = new DefaultSessionStorageEvaluator() {

        @Override
        public boolean isSessionStorageEnabled(Subject subject) {
            // save to session if we already have a session. do not create on just for saving the subject
            return subject.getSession(false) != null;
        }
    };
    sessionStorageEvaluator.setSessionStorageEnabled(false);
    subjectDAO.setSessionStorageEvaluator(sessionStorageEvaluator);
    sm.setSubjectDAO(subjectDAO);
    final User user = new UserImpl(mock(PasswordAlgorithmFactory.class), mock(Permissions.class), ImmutableMap.of());
    when(userService.load(anyString())).thenReturn(user);
    when(userService.loadById(anyString())).thenReturn(user);
    final String USERID = "123456";
    UserContext.<Void>runAs(USERID, () -> {
        final UserContext userContext = new UserContext.Factory(userService).create();
        assertThat(userContext.getUserId()).isEqualTo(USERID);
        assertThat(userContext.getUser()).isEqualTo(user);
        return null;
    });
}
Also used : DefaultSubjectDAO(org.apache.shiro.mgt.DefaultSubjectDAO) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) User(org.graylog2.plugin.database.users.User) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) DefaultSessionStorageEvaluator(org.apache.shiro.mgt.DefaultSessionStorageEvaluator) Subject(org.apache.shiro.subject.Subject) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.Test)14 Permissions (org.graylog2.shared.security.Permissions)13 UserImpl (org.graylog2.users.UserImpl)11 PasswordAlgorithmFactory (org.graylog2.security.PasswordAlgorithmFactory)8 User (org.graylog2.plugin.database.users.User)5 HashMap (java.util.HashMap)3 ViewDTO (org.graylog.plugins.views.search.views.ViewDTO)3 Role (org.graylog2.shared.users.Role)3 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)2 NotificationDto (org.graylog.events.notifications.NotificationDto)2 Configuration (org.graylog2.Configuration)2 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)2 MongoConnection (org.graylog2.database.MongoConnection)2 ValidationResult (org.graylog2.plugin.database.validators.ValidationResult)2 LdapEntry (org.graylog2.shared.security.ldap.LdapEntry)2 LdapSettings (org.graylog2.shared.security.ldap.LdapSettings)2 UserService (org.graylog2.shared.users.UserService)2 Before (org.junit.Before)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)1