Exemple #1
0
 public List<Group> getGroups() {
   List<Group> allGroups = Group.findAll();
   List<Group> answer = new ArrayList<Group>();
   for (Group g : allGroups) {
     for (User u : g.members) {
       if (u.equals(this)) {
         answer.add(g);
         break;
       }
     }
   }
   return answer;
 }
 public boolean visible(User user) {
   if (visibility == Visibility.PRIVATE) return user.equals(owner);
   if (visibility == Visibility.FRIENDS) return user.equals(owner) || user.isFriendsWith(owner);
   return true;
 }
  @Override
  public List<UserExtSource> consolidateIdentityUsingToken(PerunSession sess, String token)
      throws PerunException {

    Map<String, Object> originalIdentity = requestCache.get(token);

    if (originalIdentity == null) {
      throw new InvalidTokenException(
          "Your token for joining identities is no longer valid. Please retry from the start.");
    }

    User originalUser = (User) originalIdentity.get("user");
    User currentUser = sess.getPerunPrincipal().getUser();

    if (originalUser == null && currentUser == null) {
      IdentityUnknownException ex =
          new IdentityUnknownException(
              "Neither original or current identity is know to Perun. Please use at least one identity known to Perun.");
      ex.setLogin((String) originalIdentity.get("actor"));
      ex.setSource2((String) originalIdentity.get("extSourceName"));
      ex.setSourceType2((String) originalIdentity.get("extSourceType"));
      ex.setLogin2(sess.getPerunPrincipal().getActor());
      ex.setSource2(sess.getPerunPrincipal().getExtSourceName());
      ex.setSourceType2(sess.getPerunPrincipal().getExtSourceType());
      throw ex;
    }

    if (originalIdentity.get("extSourceName").equals(sess.getPerunPrincipal().getExtSourceName())
        && originalIdentity.get("actor").equals(sess.getPerunPrincipal().getActor())
        && originalIdentity
            .get("extSourceType")
            .equals(sess.getPerunPrincipal().getExtSourceType())) {
      IdentityIsSameException ex =
          new IdentityIsSameException(
              "You tried to join same identity with itself. Please try again but select different identity.");
      ex.setLogin(sess.getPerunPrincipal().getActor());
      ex.setSource(sess.getPerunPrincipal().getExtSourceName());
      ex.setSourceType(sess.getPerunPrincipal().getExtSourceType());
      throw ex;
    }

    if (originalUser != null && currentUser != null && originalUser.equals(currentUser)) {
      throw new IdentitiesAlreadyJoinedException("You already have both identities joined.");
    }

    if (originalUser != null && currentUser != null && !originalUser.equals(currentUser)) {
      throw new IdentityAlreadyInUseException(
          "Your identity is already associated with a different user. If you are really the same person, please contact support to help you.",
          originalUser,
          currentUser);
    }

    // merge original identity into current user
    if (originalUser == null) {
      createExtSourceAndUserExtSource(
          currentUser,
          (String) originalIdentity.get("actor"),
          (String) originalIdentity.get("extSourceName"),
          (String) originalIdentity.get("extSourceType"),
          (Integer) originalIdentity.get("extSourceLoa"));
    }

    // merge current identity into original user
    if (currentUser == null) {
      createExtSourceAndUserExtSource(
          originalUser,
          sess.getPerunPrincipal().getActor(),
          sess.getPerunPrincipal().getExtSourceName(),
          sess.getPerunPrincipal().getExtSourceType(),
          sess.getPerunPrincipal().getExtSourceLoa());
    }

    AuthzResolverBlImpl.refreshSession(sess);

    requestCache.remove(token);

    return perun.getUsersManager().getUserExtSources(sess, sess.getPerunPrincipal().getUser());
  }
Exemple #4
0
 public boolean updateUser(
     BeanFieldGroup<User> fieldGroup, String currentPass, String newClearPass)
     throws BLException, FieldGroup.CommitException, CloneNotSupportedException {
   BeanItem<User> old = fieldGroup.getItemDataSource();
   Object oldUser = old.getBean().clone();
   fieldGroup.commit();
   BeanItem<User> item = fieldGroup.getItemDataSource();
   User u = item.getBean();
   boolean userUpdated = false;
   try {
     userUpdated =
         (boolean)
             DB.execWithTransaction(
                 (db) -> {
                   User user = (User) db.session().merge(u);
                   UserManager mgr = new UserManager(db);
                   boolean updated = false;
                   if (!newClearPass.isEmpty()) {
                     boolean passwordOK = false;
                     boolean newPasswordOK = false;
                     try {
                       passwordOK = mgr.checkPassword(user, currentPass);
                       newPasswordOK = mgr.checkNewPassword(user, newClearPass);
                       if (passwordOK && newPasswordOK) {
                         mgr.setPassword(user, newClearPass);
                         updated = true;
                       } else if (!newPasswordOK) {
                         throw new BLException("This password has already been used");
                       }
                     } catch (BLException e) {
                       // do nothing
                       return false;
                     }
                   }
                   updated =
                       updated
                           || addRevisionUpdated(
                               db,
                               getEntityName(),
                               String.valueOf(u.getId()),
                               oldUser,
                               u,
                               new String[] {
                                 "nick", "name", "email", "active", "roles", "password"
                               });
                   return updated;
                 });
   } catch (Exception e) {
     getApp().getLog().error(e);
     return false;
   }
   if (userUpdated && u.equals(getApp().getUser())) {
     try {
       DB.exec(
           (db) -> {
             db.session().refresh(getApp().getUser());
             return null;
           });
     } catch (Exception e) {
       getApp().getLog().error(e);
     }
   }
   return userUpdated;
 }