예제 #1
0
  @SuppressWarnings("unchecked")
  public void updateUser(final User updatedUser) {
    String serverEncryptPassword = EncryptUtils.clientPwd2ServerPwd(updatedUser.getPassword());
    updatedUser.setPassword(serverEncryptPassword);
    User userInDb = getUser(updatedUser.getUserName());
    final boolean existed = (userInDb != null);
    if (existed) {
      userDao.merge(updatedUser);

      // 更新用户的管理范围,权限树
      if (!YuepObjectUtils.collectionEquals(userInDb.getMgmtScope(), updatedUser.getMgmtScope())) {
        mgmtScopeManager.setUserMgmtScope(updatedUser.getUserName(), updatedUser.getMgmtScope());
      }
      if (!YuepObjectUtils.collectionEquals(userInDb.getRoles(), updatedUser.getRoles())) {
        userPermissionTree.userRolePermissionChanged(updatedUser);
      }
    } else {
      userDao.saveEntity(updatedUser);
    }

    // 发送消息
    SmMessage msg = new SmMessage();
    msg.setMessageBody(updatedUser);
    if (existed) {
      msg.setMessageType(SmMessage.TYPE_USER_UPDATE);
      Map changedInfo = userInDb.getValueCompareObjectMap(updatedUser);
      msg.setAdditions(changedInfo);
    } else {
      msg.setMessageType(SmMessage.TYPE_USER_ADD);
    }
    SmCoreContext.publishMessage(msg.getName(), msg);
  }
예제 #2
0
  /**
   * 创建一个user
   *
   * @param user
   */
  public void addUser(User user) {
    String serverEncryptPwd = EncryptUtils.clientPwd2ServerPwd(user.getPassword());
    user.setPassword(serverEncryptPwd);
    userDao.saveEntity(user);

    // 发送消息
    SmMessage msg = new SmMessage();
    msg.setMessageType(SmMessage.TYPE_USER_ADD);
    msg.setMessageBody(user);
    SmCoreContext.publishMessage(msg.getName(), msg);
  }
예제 #3
0
  /**
   * 登录
   *
   * @param user
   * @param password
   */
  public void login(String user, String password) {
    // System.out.println("UserManager.login:"******"," + password);
    Session session = sessionService.getSession();
    session.setOwner(user);

    User userObj = userDao.getUniqueEntityByOneProperty("userName", user);
    if (userObj == null) throw new SmException(SmException.USER_NOT_FOUND, user);
    String serverPwd = EncryptUtils.clientPwd2ServerPwd(password);
    if (!serverPwd.equals(userObj.getPassword()))
      throw new SmException(SmException.PASSWORD_WRONG, user);

    if (userObj.getState() == User.STATE_DISABLE) throw new SmException(SmException.USER_DISABLE);

    long now = System.currentTimeMillis();
    if (userObj.getExpiredTime() != null) {
      if (now >= userObj.getExpiredTime()) {
        throw new SmException(SmException.USER_EXPIRED);
      }
    }

    if (userObj.getPasswordExpiredTime() != null) {
      if (now >= userObj.getPasswordExpiredTime()) {
        throw new SmException(SmException.PASSWORD_EXPIRED);
      }
    }

    // 更新session中的owner属性
    String clientIp = session.getIp();
    if (CollectionUtils.isNotEmpty(userObj.getIpRanges())) {
      long ip = WatchUtil.getAddrLong(clientIp);
      boolean inRange = false;
      for (IpRange ipRange : userObj.getIpRanges()) {
        if (ipRange.isInRange(ip)) {
          inRange = true;
          break;
        }
      }
      if (!inRange) throw new SmException(SmException.NOT_IN_IPRANGE);
    }

    session.setSessionState(SessionState.Active);
    // 第一次login需要初始化mgmt cache,permission cache
    Set<Long> userSessionIds = loginedUserSessions.get(user);
    if (userSessionIds == null) {
      userSessionIds = new HashSet<Long>();
      loginedUserSessions.put(user, userSessionIds);
    }
    userSessionIds.add(session.getSessionId());
    if (userSessionIds.size() == 1) {
      // 初始化2个cache
      mgmtScopeManager.setUserMgmtScope(user, userObj.getMgmtScope());
      userPermissionTree.buildUserPermissionTree(userObj);
    }
  }
예제 #4
0
  public void deleteUser(final User user) {
    Set<Long> sessions = loginedUserSessions.get(user.getUserName());
    if (CollectionUtils.isNotEmpty(sessions)) {
      throw new SmException(SmException.USER_IN_SESSION);
    }

    // 删除用户的个性化信息
    userCustomProcessor.deleteUserCustom(user.getUserName());
    userDao.deleteEntity(user);

    // 发送消息
    SmMessage msg = new SmMessage();
    msg.setMessageType(SmMessage.TYPE_USER_DELETE);
    msg.setMessageBody(user);
    SmCoreContext.publishMessage(msg.getName(), msg);
  }
예제 #5
0
 public User getUser(final String userName) {
   return userDao.getUniqueEntityByOneProperty("userName", userName);
 }
예제 #6
0
 public List<IpRange> getAllUsersIpRange() {
   return ipRangeDao.getAllEntities();
 }
예제 #7
0
 public List<User> getAllUsers() {
   return userDao.getAllEntities();
 }