Search in sources :

Example 6 with UserDetailsImpl

use of gemma.gsec.authentication.UserDetailsImpl in project Gemma by PavlidisLab.

the class AclCollectionBeforeTest method setup.

@Before
public final void setup() {
    one = super.getTestPersistentBasicExpressionExperiment();
    two = super.getTestPersistentBasicExpressionExperiment();
    securityService.makePublic(one);
    securityService.makePublic(two);
    ees = new HashSet<>();
    ees.add(one);
    ees.add(two);
    try {
        userManager.loadUserByUsername(userName);
    } catch (UsernameNotFoundException e) {
        userManager.createUser(new UserDetailsImpl("foo", userName, true, null, RandomStringUtils.randomAlphabetic(10) + "@gmail.com", "key", new Date()));
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) Date(java.util.Date) Before(org.junit.Before)

Example 7 with UserDetailsImpl

use of gemma.gsec.authentication.UserDetailsImpl in project Gemma by PavlidisLab.

the class AuditAdviceTest method testSimpleAuditCreateUpdateUser.

@Test
public void testSimpleAuditCreateUpdateUser() {
    String USERNAME = RandomStringUtils.randomAlphabetic(BaseSpringContextTest.RANDOM_STRING_LENGTH);
    String encodedPassword = passwordEncoder.encodePassword(USERNAME, USERNAME);
    UserDetailsImpl u = new UserDetailsImpl(encodedPassword, USERNAME, true, null, null, null, new Date());
    userManager.createUser(u);
    User user = (User) userService.findByUserName(USERNAME);
    List<AuditEvent> events = auditTrailService.getEvents(user);
    assertEquals("Should have just one event, a 'create'", 1, events.size());
    assertEquals(AuditAction.CREATE, events.get(0).getAction());
    assertNotNull(events.get(0).getId());
    // change something.
    user.setEmail(RandomStringUtils.randomNumeric(10));
    userService.update(user);
    events = auditTrailService.getEvents(user);
    int sizeAfterFirstUpdate = events.size();
    assertEquals("Should have a 'create' and an 'update'", 2, sizeAfterFirstUpdate);
    // debugging...
    if (!events.get(0).getAction().equals(AuditAction.CREATE)) {
        log.info("First event wasn't 'C', dumping trail");
        for (AuditEvent ae : events) {
            log.info(ae);
        }
    }
    assertEquals(AuditAction.CREATE, events.get(0).getAction());
    assertEquals(AuditAction.UPDATE, events.get(sizeAfterFirstUpdate - 1).getAction());
    // third time.
    // change something.
    user.setEmail(RandomStringUtils.randomNumeric(10));
    userService.update(user);
    events = auditTrailService.getEvents(user);
    assertEquals(3, user.getAuditTrail().getEvents().size());
    assertEquals(AuditAction.UPDATE, events.get(2).getAction());
    // cleanup
    userManager.deleteUser(USERNAME);
}
Also used : UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) User(ubic.gemma.model.common.auditAndSecurity.User) AuditEvent(ubic.gemma.model.common.auditAndSecurity.AuditEvent) BaseSpringContextTest(ubic.gemma.core.testing.BaseSpringContextTest) Test(org.junit.Test)

Example 8 with UserDetailsImpl

use of gemma.gsec.authentication.UserDetailsImpl in project Gemma by PavlidisLab.

the class ManualAuthenticationProcessingTest method before.

@Before
public void before() {
    pwd = this.randomName();
    username = this.randomName();
    try {
        userManager.loadUserByUsername(username);
    } catch (UsernameNotFoundException e) {
        String encodedPassword = passwordEncoder.encodePassword(pwd, username);
        UserDetailsImpl u = new UserDetailsImpl(encodedPassword, username, true, null, null, null, new Date());
        userManager.createUser(u);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) Date(java.util.Date) Before(org.junit.Before)

Example 9 with UserDetailsImpl

use of gemma.gsec.authentication.UserDetailsImpl in project Gemma by PavlidisLab.

the class ExecutingTaskTest method testSecurityContextManagement.

@Test
public void testSecurityContextManagement() {
    try {
        this.userManager.loadUserByUsername("ExecutingTaskTestUser");
    } catch (UsernameNotFoundException e) {
        this.userManager.createUser(new UserDetailsImpl("foo", "ExecutingTaskTestUser", true, null, "fooUser@chibi.ubc.ca", "key", new Date()));
    }
    this.runAsUser("ExecutingTaskTestUser");
    TaskCommand taskCommand = new TaskCommand();
    this.runAsAdmin();
    Task<TaskResult, TaskCommand> task = new SuccessTestTask();
    task.setTaskCommand(taskCommand);
    ExecutingTask<TaskResult> executingTask = new ExecutingTask<>(task, taskCommand);
    executingTask.setProgressAppender(progressAppender);
    executingTask.setStatusCallback(statusSecurityContextCheckerHandler);
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future<TaskResult> future = executorService.submit(executingTask);
    this.tryGetAnswer(future);
    assertEquals(taskCommand.getSecurityContext(), securityContextAfterInitialize);
    assertNotSame(taskCommand.getSecurityContext(), securityContextAfterFinish);
    assertEquals(null, securityContextAfterFail);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) ExecutorService(java.util.concurrent.ExecutorService) ExecutingTask(ubic.gemma.core.job.executor.common.ExecutingTask) Date(java.util.Date) Test(org.junit.Test) BaseSpringContextTest(ubic.gemma.core.testing.BaseSpringContextTest)

Example 10 with UserDetailsImpl

use of gemma.gsec.authentication.UserDetailsImpl in project Gemma by PavlidisLab.

the class SecurityControllerImpl method getGroupMembers.

@Override
public Collection<UserValueObject> getGroupMembers(String groupName) {
    Collection<UserValueObject> result = new HashSet<>();
    // happens if user is not in any displayed groups.
    if (StringUtils.isBlank(groupName)) {
        return result;
    }
    List<String> usersInGroup = userManager.findUsersInGroup(groupName);
    for (String userName : usersInGroup) {
        UserDetails details = userManager.loadUserByUsername(userName);
        UserValueObject uvo = new UserValueObject();
        uvo.setUserName(details.getUsername());
        if (details instanceof UserDetailsImpl) {
            uvo.setEmail(((UserDetailsImpl) details).getEmail());
        }
        uvo.setCurrentGroup(groupName);
        uvo.setInGroup(true);
        uvo.setAllowModification(true);
        /*
             * You can't remove yourself from a group, or remove users from the USER group.
             */
        if (userName.equals(userManager.getCurrentUsername()) || groupName.equals(AuthorityConstants.USER_GROUP_NAME)) {
            uvo.setAllowModification(false);
        }
        result.add(uvo);
    }
    return result;
}
Also used : UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) UserDetails(org.springframework.security.core.userdetails.UserDetails)

Aggregations

UserDetailsImpl (gemma.gsec.authentication.UserDetailsImpl)15 Date (java.util.Date)7 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)5 User (gemma.gsec.model.User)4 Before (org.junit.Before)4 Secured (org.springframework.security.access.annotation.Secured)3 JSONUtil (gemma.gsec.util.JSONUtil)2 Test (org.junit.Test)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 Transactional (org.springframework.transaction.annotation.Transactional)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 BaseSpringContextTest (ubic.gemma.core.testing.BaseSpringContextTest)2 UserExistsException (gemma.gsec.authentication.UserExistsException)1 UserGroup (gemma.gsec.model.UserGroup)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 ExecutorService (java.util.concurrent.ExecutorService)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 ExecutingTask (ubic.gemma.core.job.executor.common.ExecutingTask)1