Exemplo n.º 1
0
  @Test
  public void test() {
    final UserImpl user = new UserImpl("andrey");
    final UserImpl admin = new UserImpl("admin");
    final UserImpl mannheim = new UserImpl("mannheim");
    user.getParents().add(admin);
    user.getParents().add(mannheim);

    final OperationImpl read = new OperationImpl("read");
    final OperationImpl operation = new OperationImpl("op");
    read.getParents().add(operation);

    final ResourceImpl doc = new ResourceImpl("document");
    final ResourceImpl resource = new ResourceImpl("resource");
    doc.getParents().add(resource);

    final RuleSetImpl rules = new RuleSetImpl();
    rules.allow(admin, read, doc);

    final Rule rule = new Rule(user, read, doc, rules);
    final Collection<Rule> parents = rule.getParents();
    assertEquals(4, parents.size());

    final EvaluationStrategy eval = new BasicEvaluation();
    assertTrue(rule.value(eval));
  }
Exemplo n.º 2
0
 public void updateBib(
     final User user, final String town, final String description, final String webSite) {
   final UserImpl userImpl = (UserImpl) user;
   userImpl.setTown(town);
   userImpl.setDescription(description);
   userImpl.setWebSite(webSite);
   entityManager.merge(user);
 }
Exemplo n.º 3
0
 public void facebookConnect(final User user, final Long facebookId) throws NameClashException {
   final UserImpl user1 = (UserImpl) user;
   try {
     user1.setFacebookId(facebookId);
     entityManager.merge(user);
     entityManager.flush();
   } catch (EntityExistsException e) {
     user1.setFacebookId(null);
     throw new NameClashException();
   }
 }
Exemplo n.º 4
0
 public void updateNikePlusData(
     final User user,
     final String nikePlusEmail,
     final String nikePlusPassword,
     final String nikePlusId) {
   final UserImpl userImpl = (UserImpl) user;
   userImpl.setNikePluEmail(nikePlusEmail);
   userImpl.setNikePlusPassword(nikePlusPassword);
   userImpl.setNikePlusId(nikePlusId);
   entityManager.merge(userImpl);
 }
Exemplo n.º 5
0
 public User createUser(final String login, final String password, final String email)
     throws NameClashException, MailException {
   try {
     final UserImpl user = new UserImpl(login, password);
     if (email != null) user.setEmail(email);
     entityManager.persist(user);
     if (email != null) MailSender.sendSignupMail(login, password, email);
     return user;
   } catch (EntityExistsException e) {
     throw new NameClashException();
   }
 }
Exemplo n.º 6
0
 public void forgotPassword(final String email) throws UserNotFoundException, MailException {
   final Query query =
       entityManager.createQuery("select u from UserImpl u where u.email=:cryptedMail");
   query.setParameter("cryptedMail", CipherHelper.cipher(email));
   final List<UserImpl> list = query.getResultList();
   if (list.isEmpty()) throw new UserNotFoundException();
   for (final UserImpl user : list) {
     final String password = Helper.randomstring();
     user.setPassword(password);
     MailSender.forgotPasswordMail(user.getName(), password, email);
     entityManager.merge(user);
   }
 }
Exemplo n.º 7
0
 /** @return null if auth failed, user otherwise */
 public User authenticate(final String login, final String password, final boolean rememberMe) {
   final Query query = query("select u from UserImpl u where u.name=:user_login");
   query.setParameter("user_login", login);
   try {
     final UserImpl user = (UserImpl) query.getSingleResult();
     if (rememberMe && user.getRememberToken() == null) {
       final String token = generateToken();
       user.setRememberToken(token);
       entityManager.merge(user);
     }
     return user.checkPassword(password) ? user : null;
   } catch (NoResultException e) {
     return null;
   }
 }
Exemplo n.º 8
0
 @Override
 public int hashCode() {
   int result = createdAt != null ? createdAt.hashCode() : 0;
   result = 31 * result + (int) (id ^ (id >>> 32));
   result = 31 * result + (text != null ? text.hashCode() : 0);
   result = 31 * result + (source != null ? source.hashCode() : 0);
   result = 31 * result + (truncated ? 1 : 0);
   result = 31 * result + (entities != null ? entities.hashCode() : 0);
   result = 31 * result + (extendedEntities != null ? extendedEntities.hashCode() : 0);
   result = 31 * result + (int) (inReplyToStatusId ^ (inReplyToStatusId >>> 32));
   result = 31 * result + (int) (inReplyToUserId ^ (inReplyToUserId >>> 32));
   result = 31 * result + (inReplyToScreenName != null ? inReplyToScreenName.hashCode() : 0);
   result = 31 * result + (user != null ? user.hashCode() : 0);
   result = 31 * result + (geo != null ? geo.hashCode() : 0);
   result = 31 * result + (place != null ? place.hashCode() : 0);
   result = 31 * result + (currentUserRetweet != null ? currentUserRetweet.hashCode() : 0);
   result = 31 * result + (contributors != null ? Arrays.hashCode(contributors) : 0);
   result = 31 * result + (int) (retweetCount ^ (retweetCount >>> 32));
   result = 31 * result + (int) (favoriteCount ^ (favoriteCount >>> 32));
   result = 31 * result + (int) (replyCount ^ (replyCount >>> 32));
   result = 31 * result + (favorited ? 1 : 0);
   result = 31 * result + (retweeted ? 1 : 0);
   result = 31 * result + (lang != null ? lang.hashCode() : 0);
   result = 31 * result + (int) (descendentReplyCount ^ (descendentReplyCount >>> 32));
   result = 31 * result + (retweetedStatus != null ? retweetedStatus.hashCode() : 0);
   result = 31 * result + (quotedStatus != null ? quotedStatus.hashCode() : 0);
   result = 31 * result + (card != null ? card.hashCode() : 0);
   result = 31 * result + (possiblySensitive ? 1 : 0);
   return result;
 }
Exemplo n.º 9
0
 void remove() {
   super.remove();
   for (Enumeration en = basicMembers.elements(); en.hasMoreElements(); ) {
     RoleImpl role = (RoleImpl) en.nextElement();
     role.basicMemberOf.removeElement(this);
   }
   for (Enumeration en = reqMembers.elements(); en.hasMoreElements(); ) {
     RoleImpl role = (RoleImpl) en.nextElement();
     role.reqMemberOf.removeElement(this);
   }
 }
Exemplo n.º 10
0
 public boolean checkAndChangePassword(
     final User user, final String oldPassword, final String password) throws MailException {
   if (user.checkPassword(oldPassword)) {
     ((UserImpl) user).setPassword(password);
     entityManager.merge(user);
     final UserString email = user.getEmail();
     if (email != null)
       MailSender.sendPasswordChangeMail(user.getName().toString(), password, email.nonEscaped());
     return true;
   }
   return false;
 }
Exemplo n.º 11
0
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    StatusImpl status = (StatusImpl) o;

    if (id != status.id) return false;
    if (truncated != status.truncated) return false;
    if (inReplyToStatusId != status.inReplyToStatusId) return false;
    if (inReplyToUserId != status.inReplyToUserId) return false;
    if (retweetCount != status.retweetCount) return false;
    if (favoriteCount != status.favoriteCount) return false;
    if (replyCount != status.replyCount) return false;
    if (favorited != status.favorited) return false;
    if (retweeted != status.retweeted) return false;
    if (descendentReplyCount != status.descendentReplyCount) return false;
    if (possiblySensitive != status.possiblySensitive) return false;
    if (createdAt != null ? !createdAt.equals(status.createdAt) : status.createdAt != null)
      return false;
    if (text != null ? !text.equals(status.text) : status.text != null) return false;
    if (source != null ? !source.equals(status.source) : status.source != null) return false;
    if (entities != null ? !entities.equals(status.entities) : status.entities != null)
      return false;
    if (extendedEntities != null
        ? !extendedEntities.equals(status.extendedEntities)
        : status.extendedEntities != null) return false;
    if (inReplyToScreenName != null
        ? !inReplyToScreenName.equals(status.inReplyToScreenName)
        : status.inReplyToScreenName != null) return false;
    if (user != null ? !user.equals(status.user) : status.user != null) return false;
    if (geo != null ? !geo.equals(status.geo) : status.geo != null) return false;
    if (place != null ? !place.equals(status.place) : status.place != null) return false;
    if (currentUserRetweet != null
        ? !currentUserRetweet.equals(status.currentUserRetweet)
        : status.currentUserRetweet != null) return false;
    if (!Arrays.equals(contributors, status.contributors)) return false;
    if (lang != null ? !lang.equals(status.lang) : status.lang != null) return false;
    if (retweetedStatus != null
        ? !retweetedStatus.equals(status.retweetedStatus)
        : status.retweetedStatus != null) return false;
    if (quotedStatus != null
        ? !quotedStatus.equals(status.quotedStatus)
        : status.quotedStatus != null) return false;
    return !(card != null ? !card.equals(status.card) : status.card != null);
  }
Exemplo n.º 12
0
 public void updateEmail(final User user, final String email) {
   ((UserImpl) user).setEmail(email.length() > 0 ? email : null);
   entityManager.merge(user);
 }
Exemplo n.º 13
0
 public void forgetMe(final User user) {
   ((UserImpl) user).setRememberToken(null);
   entityManager.merge(user);
 }
  @Override
  public void updateFromLdap(
      User user, LdapEntry userEntry, LdapSettings ldapSettings, String username) {
    final String displayNameAttribute = ldapSettings.getDisplayNameAttribute();
    final String fullName = firstNonNull(userEntry.get(displayNameAttribute), username);

    user.setName(username);
    user.setFullName(fullName);
    user.setExternal(true);

    final String email = userEntry.getEmail();
    if (isNullOrEmpty(email)) {
      LOG.debug(
          "No email address found for user {} in LDAP. Using {}@localhost", username, username);
      user.setEmail(username + "@localhost");
    } else {
      user.setEmail(email);
    }

    // TODO This is a crude hack until we have a proper way to distinguish LDAP users from normal
    // users
    if (isNullOrEmpty(user.getHashedPassword())) {
      ((UserImpl) user).setHashedPassword("User synced from LDAP.");
    }

    if (user.getPermissions() == null) {
      user.setPermissions(Lists.newArrayList(RestPermissions.userSelfEditPermissions(username)));
    } else {
      user.setPermissions(
          Lists.newArrayList(
              Sets.union(
                  RestPermissions.userSelfEditPermissions(username),
                  Sets.newHashSet(user.getPermissions()))));
    }

    // map ldap groups to user roles, if the mapping is present
    final Set<String> translatedRoleIds =
        Sets.newHashSet(
            Sets.union(
                Sets.newHashSet(ldapSettings.getDefaultGroupId()),
                ldapSettings.getAdditionalDefaultGroupIds()));
    if (!userEntry.getGroups().isEmpty()) {
      try {
        final Map<String, Role> roleNameToRole = roleService.loadAllLowercaseNameMap();
        for (String ldapGroupName : userEntry.getGroups()) {
          final String roleName = ldapSettings.getGroupMapping().get(ldapGroupName);
          if (roleName == null) {
            LOG.warn("User {}: No group mapping for ldap group <{}>", username, ldapGroupName);
            continue;
          }
          final Role role = roleNameToRole.get(roleName.toLowerCase());
          if (role != null) {
            LOG.warn(
                "User {}: Mapping ldap group <{}> to role <{}>",
                username,
                ldapGroupName,
                role.getName());
            translatedRoleIds.add(role.getId());
          } else {
            LOG.warn("User {}: No role found for ldap group <{}>", username, ldapGroupName);
          }
        }

      } catch (NotFoundException e) {
        LOG.error("Unable to load user roles", e);
      }
    }
    user.setRoleIds(translatedRoleIds);
  }