/**
   * Extracts user groups from {@link SecurityRealm}.
   *
   * @param userId
   * @return List of effective groups. Null if there's no info
   */
  private static @CheckForNull List<String> getAuthoritiesFromRealm(@Nonnull String userId) {
    final Jenkins instance = Jenkins.getInstance();
    if (instance == null) {
      return null; // Jenkins has not been started yet
    }

    @CheckForNull UserDetails userDetails = null;
    try {
      final SecurityRealm sr = instance.getSecurityRealm();
      userDetails = sr.loadUserByUsername(userId);
    } catch (DataAccessException ex) {
      // fallback to null handler
    } catch (UsernameNotFoundException ex) {
      // fallback to null handler
    }

    if (userDetails == null) {
      return null;
    }

    GrantedAuthority[] authorities = userDetails.getAuthorities();
    List<String> authorityList = new ArrayList<String>(authorities.length);
    for (GrantedAuthority auth : authorities) {
      authorityList.add(auth.getAuthority());
    }
    return authorityList;
  }
コード例 #2
0
  public void testFill() throws Exception {
    Locale.setDefault(Locale.ENGLISH);

    // make sure the recorder is where we think it is, it contains the palette selection
    tester.assertComponent("userForm:roles:roles:recorder", Recorder.class);

    // try to add a new user
    FormTester form = tester.newFormTester("userForm");
    form.setValue("username", "user");
    form.setValue("password", "pwd");
    form.setValue("confirmPassword", "pwd");
    // note: use a known role, there is no way to add a new role using wickettester support
    form.setValue("roles:roles:recorder", dao.getRoles().get(0));
    form.submit("save");

    tester.assertErrorMessages(new String[0]);
    tester.assertRenderedPage(UserPage.class);

    dao.reload();
    UserDetails user = dao.loadUserByUsername("user");
    assertEquals("pwd", user.getPassword());
    assertEquals(1, user.getAuthorities().length);
  }
コード例 #3
0
  public void testLoadUserByUsername() {
    User u =
        new User() {
          @Override
          public boolean hasPermission(PermissionName permission) {
            return true;
          }
        };
    u.setUserName(USER_NAME);
    u.setUniqueId();

    CoreContext coreContext = createMock(CoreContext.class);
    AcdContext acdContext = createMock(AcdContext.class);
    StandardUserDetailsService uds = new StandardUserDetailsService();
    uds.setCoreContext(coreContext);
    uds.setAcdContext(acdContext);

    coreContext.loadUserByUserNameOrAlias(USER_NAME);
    expectLastCall().andReturn(u);

    acdContext.getUsersWithAgents();
    expectLastCall().andReturn(Collections.emptyList());
    replay(coreContext, acdContext);

    // load the user details
    UserDetails details = uds.loadUserByUsername(USER_NAME);
    assertEquals(USER_NAME, details.getUsername());
    GrantedAuthority[] authorities = details.getAuthorities();

    assertTrue(contains(authorities, UserRole.Admin.toAuth()));
    assertTrue(contains(authorities, UserRole.User.toAuth()));
    assertFalse(contains(authorities, UserRole.AcdAgent.toAuth()));
    assertFalse(contains(authorities, UserRole.AcdSupervisor.toAuth()));
    assertTrue(contains(authorities, UserRole.AttendantAdmin.toAuth()));

    verify(coreContext, acdContext);
  }