Ejemplo n.º 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);
  }
Ejemplo n.º 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);
  }
Ejemplo n.º 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);
    }
  }
Ejemplo n.º 4
0
  @SuppressWarnings("unchecked")
  @Override
  public List<Object> collectData() {
    // 读取用户名,密码
    String pwd = new String(pwdTextField.getPassword());
    String user = userField.getText();
    if (StringUtils.isEmpty(pwd) || StringUtils.isEmpty(user)) {
      DialogUtils.showErrorDialog(this.getWindow(), "smmanager.modifyselfpassword.input is null");
      return null;
    }

    List datas = new ArrayList();
    datas.add(user);
    // 加密传输
    String encryptPwd = EncryptUtils.setEncrypt(pwd, EncryptUtils.MAGIC_KEY_CLIENT);
    datas.add(encryptPwd);
    return datas;
  }
Ejemplo n.º 5
0
 /** 初始化数据,创建默认账号 */
 private void initDefaultUserData() {
   List<User> users = getAllUsers();
   if (CollectionUtils.isEmpty(users)) {
     User admin = new User();
     admin.setUserName("admin");
     admin.setPassword(EncryptUtils.setEncrypt("admin", EncryptUtils.MAGIC_KEY_CLIENT));
     // 设置缺省的role
     List<String> roles = new ArrayList<String>();
     roles.add("admin");
     admin.setRoles(roles);
     // 设置缺省的管理范围
     MoCore moCore =
         SmCoreContext.getLocalService(MoCoreModuleConstants.MOCORE_LOCAL_SERVICE, MoCore.class);
     MoNaming rootMo = moCore.getRootMo().getMoNaming();
     List<MoNaming> mgmtScope = new ArrayList<MoNaming>();
     mgmtScope.add(rootMo);
     admin.setMgmtScope(mgmtScope);
     addUser(admin);
   }
 }