/**
   * @return <code>null</code> if failed. The browser is already redirected to retry by the time
   *     this method returns. a valid {@link User} object if the user creation was successful.
   */
  private User createAccount(StaplerRequest req, StaplerResponse rsp, String formView)
      throws ServletException, IOException {

    SignupInfo si = new SignupInfo(req);

    String puid = authenticateInWwpass(si.ticket, certFile, keyFile);

    try {
      if (loadUserByUsername(puid) != null) {
        si.errorMessages.add(Messages.WwpassSecurityRealm_PuidIsAlreadyTaken());
      }
    } catch (UsernameNotFoundException e) {

    }

    if (si.nickname == null || si.nickname.length() == 0)
      si.errorMessages.add(Messages.WwpassSecurityRealm_NicknameIsRequired());
    else {
      User user = User.get(si.nickname, false);
      if (null != user)
        if (user.getProperty(WwpassIdentity.class) != null)
          si.errorMessages.add(Messages.WwpassSecurityRealm_NicknameIsAlreadyTaken());
    }

    if (si.fullname == null || si.fullname.length() == 0)
      si.errorMessages.add(Messages.WwpassSecurityRealm_FullnameIsRequired());
    else {
      User user = User.get(si.fullname, false);
      if (null != user)
        if (user.getProperty(WwpassIdentity.class) != null)
          si.errorMessages.add(Messages.WwpassSecurityRealm_FullnameIsAlreadyTaken());
    }

    if (si.email == null || !si.email.contains("@"))
      si.errorMessages.add(Messages.WwpassSecurityRealm_InvalidEmailAddress());

    if (!si.errorMessages.isEmpty()) {
      // failed. ask the user to try again.
      req.setAttribute("data", si);
      req.getView(this, formView).forward(req, rsp);
      return null;
    }

    // register the user
    WwpassIdentity id = new WwpassIdentity(puid);
    id.populate(si);

    User user = createAccount(id);
    id.updateProfile(user);

    user.save();
    return user;
  }
Пример #2
0
    @Test public void privateView() throws Exception {
        j.createFreeStyleProject("project1");
        User user = User.get("me", true); // create user

        WebClient wc = j.createWebClient();
        HtmlPage userPage = wc.goTo("user/me");
        HtmlAnchor privateViewsLink = userPage.getFirstAnchorByText("My Views");
        assertNotNull("My Views link not available", privateViewsLink);

        HtmlPage privateViewsPage = (HtmlPage) privateViewsLink.click();

        Text viewLabel = (Text) privateViewsPage.getFirstByXPath("//div[@class='tabBar']//div[@class='tab active']/a/text()");
        assertTrue("'All' view should be selected", viewLabel.getTextContent().contains(Hudson_ViewName()));

        View listView = listView("listView");

        HtmlPage newViewPage = wc.goTo("user/me/my-views/newView");
        HtmlForm form = newViewPage.getFormByName("createItem");
        form.getInputByName("name").setValueAttribute("proxy-view");
        ((HtmlRadioButtonInput) form.getInputByValue("hudson.model.ProxyView")).setChecked(true);
        HtmlPage proxyViewConfigurePage = j.submit(form);
        View proxyView = user.getProperty(MyViewsProperty.class).getView("proxy-view");
        assertNotNull(proxyView);
        form = proxyViewConfigurePage.getFormByName("viewConfig");
        form.getSelectByName("proxiedViewName").setSelectedAttribute("listView", true);
        j.submit(form);

        assertTrue(proxyView instanceof ProxyView);
        assertEquals(((ProxyView) proxyView).getProxiedViewName(), "listView");
        assertEquals(((ProxyView) proxyView).getProxiedView(), listView);
    }
  private void addUserTriggeringTheBuild(
      AbstractBuild<?, ?> build,
      Set<InternetAddress> recipientAddresses,
      Set<InternetAddress> ccAddresses,
      EnvVars env,
      BuildListener listener) {
    User user = getByUserIdCause(build);
    if (user == null) {
      user = getByLegacyUserCause(build);
    }

    if (user != null) {
      String adrs = user.getProperty(Mailer.UserProperty.class).getAddress();
      if (adrs != null) {
        addAddressesFromRecipientList(recipientAddresses, ccAddresses, adrs, env, listener);
      } else {
        listener
            .getLogger()
            .println(
                "Failed to send e-mail to "
                    + user.getFullName()
                    + " because no e-mail address is known, and no default e-mail domain is configured");
      }
    }
  }
 /**
  * All users who can login to the system.
  */
 public List<User> getAllUsers() {
     List<User> r = new ArrayList<User>();
     for (User u : User.getAll()) {
         if(u.getProperty(Details.class)!=null)
             r.add(u);
     }
     Collections.sort(r);
     return r;
 }
  @Test
  public void doResolveWhenUsingExplicitlUserEmail() {
    final String testEmail = "*****@*****.**";

    when(user.getProperty(Mailer.UserProperty.class))
        .thenReturn(new Mailer.UserProperty(testEmail));

    final String address = MailAddressResolver.resolveFast(user);
    assertEquals(testEmail, address);
  }
 @Override
 public Details loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
     User u = User.get(username,false);
     Details p = u!=null ? u.getProperty(Details.class) : null;
     if(p==null)
         throw new UsernameNotFoundException("Password is not set: "+username);
     if(p.getUser()==null)
         throw new AssertionError();
     return p;
 }
  /** Associates the OpenID identity of the user with {@link #realm}. */
  private void associateUserWithOpenId(User u) throws Exception {
    WebClient wc = new WebClient().login(u.getId(), u.getId() /*assumes password==name*/);

    // Associate an OpenID with an existing user
    HtmlPage associated =
        wc.goTo("federatedLoginService/openid/startAssociate?openid=" + openid.url);
    assertTrue(
        associated.getDocumentURI().endsWith("federatedLoginService/openid/onAssociationSuccess"));
    OpenIdUserProperty p = u.getProperty(OpenIdUserProperty.class);
    assertEquals(1, p.getIdentifiers().size());
    assertEquals(openid.getUserIdentity(), p.getIdentifiers().iterator().next());
  }
  /** Try to make this user a super-user */
  private void tryToMakeAdmin(User u) throws IOException {
    WwpassIdentity p = u.getProperty(WwpassIdentity.class);
    p.activate();
    u.save();
    AuthorizationStrategy as = Jenkins.getInstance().getAuthorizationStrategy();

    for (PermissionAdder adder : Jenkins.getInstance().getExtensionList(PermissionAdder.class)) {
      if (adder.add(as, u, Jenkins.ADMINISTER)) {
        return;
      }
    }
    LOGGER.severe(
        "Admin permission wasn't added for user: "******", ID: " + u.getId());
  }
  @Override
  public WwpassIdentity loadUserByUsername(String puid)
      throws UsernameNotFoundException, DataAccessException {
    Collection<User> all = User.getAll();

    for (User u : all) {
      WwpassIdentity p = u.getProperty(WwpassIdentity.class);
      if (puid.equals(p != null ? p.getPuid() : null)) {
        return p;
      }
    }

    throw new UsernameNotFoundException("There is no any user with: " + puid);
  }
Пример #10
0
    @Test public void deleteView() throws Exception {
        WebClient wc = j.createWebClient();

        ListView v = listView("list");
        HtmlPage delete = wc.getPage(v, "delete");
        j.submit(delete.getFormByName("delete"));
        assertNull(j.jenkins.getView("list"));

        User user = User.get("user", true);
        MyViewsProperty p = user.getProperty(MyViewsProperty.class);
        v = new ListView("list", p);
        p.addView(v);
        delete = wc.getPage(v, "delete");
        j.submit(delete.getFormByName("delete"));
        assertNull(p.getView("list"));

    }
Пример #11
0
 /**
  * Add the score to the users that have committed code in the change set
  *
  * @param score the score that the build was worth
  * @param accountableBuilds the builds for which the {@code score} is awarded for.
  * @throws IOException thrown if the property could not be added to the user object.
  * @return true, if any user scores was updated; false, otherwise
  */
 private boolean updateUserScores(
     Set<User> players, double score, List<AbstractBuild<?, ?>> accountableBuilds)
     throws IOException {
   if (score != 0) {
     for (User user : players) {
       UserScoreProperty property = user.getProperty(UserScoreProperty.class);
       if (property == null) {
         property = new UserScoreProperty();
         user.addProperty(property);
       }
       if (property.isParticipatingInGame()) {
         property.setScore(property.getScore() + score);
         property.rememberAccountableBuilds(accountableBuilds, score);
       }
       user.save();
     }
   }
   return (!players.isEmpty());
 }
    /**
     * @return
     *      null if failed. The browser is already redirected to retry by the time this method returns.
     *      a valid {@link User} object if the user creation was successful.
     */
    private User createAccount(StaplerRequest req, StaplerResponse rsp, boolean selfRegistration, String formView) throws ServletException, IOException {
        // form field validation
        // this pattern needs to be generalized and moved to stapler
        SignupInfo si = new SignupInfo(req);

        if(selfRegistration && !validateCaptcha(si.captcha))
            si.errorMessage = "Text didn't match the word shown in the image";

        if(si.password1 != null && !si.password1.equals(si.password2))
            si.errorMessage = "Password didn't match";

        if(!(si.password1 != null && si.password1.length() != 0))
            si.errorMessage = "Password is required";

        if(si.username==null || si.username.length()==0)
            si.errorMessage = "User name is required";
        else {
            User user = User.get(si.username);
            if(user.getProperty(Details.class)!=null)
                si.errorMessage = "User name is already taken. Did you forget the password?";
        }

        if(si.fullname==null || si.fullname.length()==0)
            si.fullname = si.username;

        if(si.email==null || !si.email.contains("@"))
            si.errorMessage = "Invalid e-mail address";

        if(si.errorMessage!=null) {
            // failed. ask the user to try again.
            req.setAttribute("data",si);
            req.getView(this, formView).forward(req,rsp);
            return null;
        }

        // register the user
        User user = createAccount(si.username,si.password1);
        user.addProperty(new Mailer.UserProperty(si.email));
        user.setFullName(si.fullname);
        user.save();
        return user;
    }
Пример #13
0
  /**
   * Tests {@link Team#doImportViewsSubmit(String, org.kohsuke.stapler.StaplerRequest,
   * org.kohsuke.stapler.StaplerResponse)}.
   *
   * @throws Exception if so
   */
  public void testDoImportViewsSubmit() throws Exception {
    FreeStyleProject p = createFreeStyleProject();
    User user = User.get("bob", true);
    MyViewsProperty property = user.getProperty(MyViewsProperty.class);
    ListView view1 = new ListView("view1");
    view1.add(p);
    property.addView(view1);
    ListView view2 = new ListView("view2");
    property.addView(view2);
    user.save();

    PluginImpl.getInstance().addTeam(new Team("Team1", "Description"));
    Team team = PluginImpl.getInstance().getTeams().get("Team1");
    StaplerRequest request = mock(StaplerRequest.class);
    StaplerResponse response = mock(StaplerResponse.class);

    team.doImportViewsSubmit(user.getId(), request, response);

    verify(response).sendRedirect2(Matchers.contains(team.getUrl()));

    TeamViewsProperty views = team.getProperty(TeamViewsProperty.class);
    Collection<View> collection = views.getViews();
    assertEquals(3, collection.size());

    boolean found1 = false;
    boolean found2 = false;
    for (View next : collection) {
      if (next.getViewName().equals("view1")) {
        found1 = true;
        assertNotSame(view1, next);
        assertEquals(1, next.getItems().size());
      } else if (next.getViewName().equals("view2")) {
        found2 = true;
        assertNotSame(view2, next);
      }
    }
    assertTrue(found1);
    assertTrue(found2);
  }
 /**
  * Computes if this Jenkins has some user accounts configured.
  *
  * <p>This is used to check for the initial
  */
 private static boolean hasSomeUser() {
   for (User u : User.getAll()) if (u.getProperty(WwpassIdentity.class) != null) return true;
   return false;
 }
Пример #15
0
 private boolean isMailerPropertySet(User user) {
   UserProperty property = user.getProperty(Mailer.UserProperty.class);
   return property != null && property.hasExplicitlyConfiguredAddress();
 }