예제 #1
0
파일: UserApp.java 프로젝트: bluemir/hive
  /**
   * 대표 메일로 설정하기
   *
   * @param id
   * @return
   */
  @Transactional
  public static Result setAsMainEmail(Long id) {
    User currentUser = currentUser();
    Email email = Email.find.byId(id);

    if (currentUser == null || currentUser.isAnonymous() || email == null) {
      return forbidden(ErrorViews.NotFound.render());
    }

    if (!AccessControl.isAllowed(currentUser, email.user.asResource(), Operation.UPDATE)) {
      return forbidden(ErrorViews.Forbidden.render(Messages.get("error.forbidden")));
    }

    String oldMainEmail = currentUser.email;
    currentUser.email = email.email;
    currentUser.removeEmail(email);
    currentUser.update();

    Email newSubEmail = new Email();
    newSubEmail.valid = true;
    newSubEmail.email = oldMainEmail;
    newSubEmail.user = currentUser;
    currentUser.addEmail(newSubEmail);

    return redirect(routes.UserApp.editUserInfoForm());
  }
예제 #2
0
파일: UserApp.java 프로젝트: bluemir/hive
  /**
   * 사용자 정보 수정
   *
   * @return
   */
  @With(AnonymousCheckAction.class)
  @Transactional
  public static Result editUserInfo() {
    Form<User> userForm = new Form<>(User.class).bindFromRequest("name", "email");
    String newEmail = userForm.data().get("email");
    String newName = userForm.data().get("name");
    User user = UserApp.currentUser();

    if (StringUtils.isEmpty(newEmail)) {
      userForm.reject("email", "user.wrongEmail.alert");
    } else {
      if (!StringUtils.equals(user.email, newEmail) && User.isEmailExist(newEmail)) {
        userForm.reject("email", "user.email.duplicate");
      }
    }

    if (userForm.error("email") != null) {
      flash(Constants.WARNING, userForm.error("email").message());
      return badRequest(edit.render(userForm, user));
    }
    user.email = newEmail;
    user.name = newName;

    try {
      Long avatarId = Long.valueOf(userForm.data().get("avatarId"));
      if (avatarId != null) {
        Attachment attachment = Attachment.find.byId(avatarId);
        String primary = attachment.mimeType.split("/")[0].toLowerCase();

        if (attachment.size > AVATAR_FILE_LIMIT_SIZE) {
          userForm.reject("avatarId", "user.avatar.fileSizeAlert");
        }

        if (primary.equals("image")) {
          Attachment.deleteAll(currentUser().avatarAsResource());
          attachment.moveTo(currentUser().avatarAsResource());
        }
      }
    } catch (NumberFormatException ignored) {
    }

    Email.deleteOtherInvalidEmails(user.email);
    user.update();
    return redirect(
        routes.UserApp.userInfo(user.loginId, DEFAULT_GROUP, DAYS_AGO, DEFAULT_SELECTED_TAB));
  }
예제 #3
0
  public static User create(final AuthUser authUser) {
    final User user = new User();
    user.roles =
        Collections.singletonList(SecurityRole.findByRoleName(controllers.Application.USER_ROLE));
    // user.permissions = new ArrayList<UserPermission>();
    // user.permissions.add(UserPermission.findByValue("printers.edit"));
    user.active = true;
    user.lastLogin = new Date();
    user.linkedAccounts = Collections.singletonList(LinkedAccount.create(authUser));

    if (authUser instanceof EmailIdentity) {
      final EmailIdentity identity = (EmailIdentity) authUser;
      // Remember, even when getting them from FB & Co., emails should be
      // verified within the application as a security breach there might
      // break your security as well!
      user.email = identity.getEmail();
      user.emailValidated = false;
    }

    if (authUser instanceof NameIdentity) {
      final NameIdentity identity = (NameIdentity) authUser;
      final String name = identity.getName();
      if (name != null) {
        user.name = name;
      }
    }

    if (authUser instanceof FirstLastNameIdentity) {
      final FirstLastNameIdentity identity = (FirstLastNameIdentity) authUser;
      final String firstName = identity.getFirstName();
      final String lastName = identity.getLastName();
      if (firstName != null) {
        user.firstName = firstName;
      }
      if (lastName != null) {
        user.lastName = lastName;
      }
    }

    MorphiaObject.datastore.save(user);

    // user.saveManyToManyAssociations("roles");
    // user.saveManyToManyAssociations("permissions");
    return user;
  }