Search in sources :

Example 1 with UserRole

use of top.hcode.hoj.pojo.entity.user.UserRole in project HOJ by HimitZH.

the class AdminUserController method editUser.

@PutMapping("/edit-user")
@RequiresPermissions("user_admin")
@RequiresAuthentication
@Transactional(rollbackFor = Exception.class)
public CommonResult editUser(@RequestBody Map<String, Object> params, HttpServletRequest request) {
    String username = (String) params.get("username");
    String uid = (String) params.get("uid");
    String realname = (String) params.get("realname");
    String email = (String) params.get("email");
    String password = (String) params.get("password");
    int type = (int) params.get("type");
    int status = (int) params.get("status");
    boolean setNewPwd = (boolean) params.get("setNewPwd");
    UpdateWrapper<UserInfo> updateWrapper1 = new UpdateWrapper<>();
    updateWrapper1.eq("uuid", uid).set("username", username).set("realname", realname).set("email", email).set(setNewPwd, "password", SecureUtil.md5(password)).set("status", status);
    boolean result1 = userInfoService.update(updateWrapper1);
    QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
    userRoleQueryWrapper.eq("uid", uid);
    UserRole userRole = userRoleService.getOne(userRoleQueryWrapper, false);
    boolean result2 = false;
    int oldType = userRole.getRoleId().intValue();
    if (userRole.getRoleId().intValue() != type) {
        userRole.setRoleId(Long.valueOf(type));
        result2 = userRoleService.updateById(userRole);
    }
    if (result1) {
        // 需要重新登录
        userRoleService.deleteCache(uid, true);
    } else if (result2) {
        // 需要重新授权
        userRoleService.deleteCache(uid, false);
    }
    if (result2) {
        // 获取当前登录的用户
        HttpSession session = request.getSession();
        UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
        String title = "权限变更通知(Authority Change Notice)";
        String content = userRoleService.getAuthChangeContent(oldType, type);
        adminSysNoticeService.addSingleNoticeToUser(userRolesVo.getUid(), uid, title, content, "Sys");
    }
    return CommonResult.successResponse(null, "修改成功!");
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UserRole(top.hcode.hoj.pojo.entity.user.UserRole) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with UserRole

use of top.hcode.hoj.pojo.entity.user.UserRole in project HOJ by HimitZH.

the class AdminUserManager method insertBatchUser.

@Transactional(rollbackFor = Exception.class)
public void insertBatchUser(List<List<String>> users) throws StatusFailException {
    List<UserInfo> userInfoList = new LinkedList<>();
    List<UserRole> userRoleList = new LinkedList<>();
    List<UserRecord> userRecordList = new LinkedList<>();
    if (users != null) {
        for (List<String> user : users) {
            String uuid = IdUtil.simpleUUID();
            UserInfo userInfo = new UserInfo().setUuid(uuid).setUsername(user.get(0)).setPassword(SecureUtil.md5(user.get(1))).setEmail(StringUtils.isEmpty(user.get(2)) ? null : user.get(2));
            if (user.size() >= 4) {
                String realname = user.get(3);
                if (!StringUtils.isEmpty(realname)) {
                    userInfo.setRealname(user.get(3));
                }
            }
            if (user.size() >= 5) {
                String gender = user.get(4);
                if ("male".equals(gender.toLowerCase()) || "0".equals(gender)) {
                    userInfo.setGender("male");
                } else if ("female".equals(gender.toLowerCase()) || "1".equals(gender)) {
                    userInfo.setGender("female");
                }
            }
            if (user.size() >= 6) {
                String nickname = user.get(5);
                if (!StringUtils.isEmpty(nickname)) {
                    userInfo.setNickname(nickname);
                }
            }
            if (user.size() >= 7) {
                String school = user.get(6);
                if (!StringUtils.isEmpty(school)) {
                    userInfo.setSchool(school);
                }
            }
            userInfoList.add(userInfo);
            userRoleList.add(new UserRole().setRoleId(1002L).setUid(uuid));
            userRecordList.add(new UserRecord().setUid(uuid));
        }
        boolean result1 = userInfoEntityService.saveBatch(userInfoList);
        boolean result2 = userRoleEntityService.saveBatch(userRoleList);
        boolean result3 = userRecordEntityService.saveBatch(userRecordList);
        if (result1 && result2 && result3) {
            // 异步同步系统通知
            List<String> uidList = userInfoList.stream().map(UserInfo::getUuid).collect(Collectors.toList());
            adminNoticeManager.syncNoticeToNewRegisterBatchUser(uidList);
        } else {
            throw new StatusFailException("删除失败");
        }
    } else {
        throw new StatusFailException("插入的用户数据不能为空!");
    }
}
Also used : UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) UserRole(top.hcode.hoj.pojo.entity.user.UserRole) UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) LinkedList(java.util.LinkedList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with UserRole

use of top.hcode.hoj.pojo.entity.user.UserRole in project HOJ by HimitZH.

the class AdminUserController method insertBatchUser.

@PostMapping("/insert-batch-user")
@RequiresPermissions("user_admin")
@RequiresAuthentication
@Transactional(rollbackFor = Exception.class)
public CommonResult insertBatchUser(@RequestBody Map<String, Object> params) {
    List<List<String>> users = (List<List<String>>) params.get("users");
    List<UserInfo> userInfoList = new LinkedList<>();
    List<UserRole> userRoleList = new LinkedList<>();
    List<UserRecord> userRecordList = new LinkedList<>();
    if (users != null) {
        for (List<String> user : users) {
            String uuid = IdUtil.simpleUUID();
            UserInfo userInfo = new UserInfo().setUuid(uuid).setUsername(user.get(0)).setPassword(SecureUtil.md5(user.get(1))).setEmail(StringUtils.isEmpty(user.get(2)) ? null : user.get(2));
            if (user.size() >= 4) {
                String realname = user.get(3);
                if (!StringUtils.isEmpty(realname)) {
                    userInfo.setRealname(user.get(3));
                }
            }
            if (user.size() >= 5) {
                String gender = user.get(4);
                if ("male".equals(gender.toLowerCase()) || "0".equals(gender)) {
                    userInfo.setGender("male");
                } else if ("female".equals(gender.toLowerCase()) || "1".equals(gender)) {
                    userInfo.setGender("female");
                }
            }
            if (user.size() >= 6) {
                String nickname = user.get(5);
                if (!StringUtils.isEmpty(nickname)) {
                    userInfo.setNickname(nickname);
                }
            }
            if (user.size() >= 7) {
                String school = user.get(6);
                if (!StringUtils.isEmpty(school)) {
                    userInfo.setSchool(school);
                }
            }
            userInfoList.add(userInfo);
            userRoleList.add(new UserRole().setRoleId(1002L).setUid(uuid));
            userRecordList.add(new UserRecord().setUid(uuid));
        }
        boolean result1 = userInfoService.saveBatch(userInfoList);
        boolean result2 = userRoleService.saveBatch(userRoleList);
        boolean result3 = userRecordService.saveBatch(userRecordList);
        if (result1 && result2 && result3) {
            // 异步同步系统通知
            List<String> uidList = userInfoList.stream().map(UserInfo::getUuid).collect(Collectors.toList());
            adminSysNoticeService.syncNoticeToNewRegisterBatchUser(uidList);
            return CommonResult.successResponse(null, "添加成功!");
        } else {
            return CommonResult.errorResponse("删除失败");
        }
    } else {
        return CommonResult.errorResponse("插入的用户数据不能为空!");
    }
}
Also used : UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) UserRole(top.hcode.hoj.pojo.entity.user.UserRole) UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with UserRole

use of top.hcode.hoj.pojo.entity.user.UserRole in project HOJ by HimitZH.

the class AdminUserController method generateUser.

@PostMapping("/generate-user")
@RequiresPermissions("user_admin")
@RequiresAuthentication
@Transactional(rollbackFor = Exception.class)
public CommonResult generateUser(@RequestBody Map<String, Object> params) {
    String prefix = (String) params.getOrDefault("prefix", "");
    String suffix = (String) params.getOrDefault("suffix", "");
    int numberFrom = (int) params.getOrDefault("number_from", 1);
    int numberTo = (int) params.getOrDefault("number_to", 10);
    int passwordLength = (int) params.getOrDefault("password_length", 6);
    List<UserInfo> userInfoList = new LinkedList<>();
    List<UserRole> userRoleList = new LinkedList<>();
    List<UserRecord> userRecordList = new LinkedList<>();
    // 存储账号密码放入redis中,等待导出excel
    HashMap<String, Object> userInfo = new HashMap<>();
    for (int num = numberFrom; num <= numberTo; num++) {
        String uuid = IdUtil.simpleUUID();
        String password = RandomUtil.randomString(passwordLength);
        String username = prefix + num + suffix;
        userInfoList.add(new UserInfo().setUuid(uuid).setUsername(username).setPassword(SecureUtil.md5(password)));
        userInfo.put(username, password);
        userRoleList.add(new UserRole().setRoleId(1002L).setUid(uuid));
        userRecordList.add(new UserRecord().setUid(uuid));
    }
    boolean result1 = userInfoService.saveBatch(userInfoList);
    boolean result2 = userRoleService.saveBatch(userRoleList);
    boolean result3 = userRecordService.saveBatch(userRecordList);
    if (result1 && result2 && result3) {
        String key = IdUtil.simpleUUID();
        // 存储半小时
        redisUtils.hmset(key, userInfo, 1800);
        // 异步同步系统通知
        List<String> uidList = userInfoList.stream().map(UserInfo::getUuid).collect(Collectors.toList());
        adminSysNoticeService.syncNoticeToNewRegisterBatchUser(uidList);
        return CommonResult.successResponse(MapUtil.builder().put("key", key).map(), "生成指定用户成功!");
    } else {
        return CommonResult.errorResponse("生成指定用户失败!");
    }
}
Also used : UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) UserRole(top.hcode.hoj.pojo.entity.user.UserRole) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with UserRole

use of top.hcode.hoj.pojo.entity.user.UserRole in project HOJ by HimitZH.

the class AdminUserManager method editUser.

public void editUser(AdminEditUserDto adminEditUserDto) throws StatusFailException {
    String username = adminEditUserDto.getUsername();
    String uid = adminEditUserDto.getUid();
    String realname = adminEditUserDto.getRealname();
    String email = adminEditUserDto.getEmail();
    String password = adminEditUserDto.getPassword();
    int type = adminEditUserDto.getType();
    int status = adminEditUserDto.getStatus();
    boolean setNewPwd = adminEditUserDto.getSetNewPwd();
    String titleName = adminEditUserDto.getTitleName();
    String titleColor = adminEditUserDto.getTitleColor();
    if (!StringUtils.isEmpty(realname) && realname.length() > 50) {
        throw new StatusFailException("真实姓名的长度不能超过50位");
    }
    if (!StringUtils.isEmpty(titleName) && titleName.length() > 20) {
        throw new StatusFailException("头衔的长度建议不要超过20位");
    }
    if (!StringUtils.isEmpty(password) && (password.length() < 6 || password.length() > 20)) {
        throw new StatusFailException("密码长度建议为6~20位!");
    }
    if (username.length() > 20) {
        throw new StatusFailException("用户名长度建议不能超过20位!");
    }
    UpdateWrapper<UserInfo> userInfoUpdateWrapper = new UpdateWrapper<>();
    userInfoUpdateWrapper.eq("uuid", uid).set("username", username).set("realname", realname).set("email", email).set(setNewPwd, "password", SecureUtil.md5(password)).set("title_name", titleName).set("title_color", titleColor).set("status", status);
    boolean updateUserInfo = userInfoEntityService.update(userInfoUpdateWrapper);
    QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
    userRoleQueryWrapper.eq("uid", uid);
    UserRole userRole = userRoleEntityService.getOne(userRoleQueryWrapper, false);
    boolean changeUserRole = false;
    int oldType = userRole.getRoleId().intValue();
    if (userRole.getRoleId().intValue() != type) {
        userRole.setRoleId((long) type);
        changeUserRole = userRoleEntityService.updateById(userRole);
        if (type == 1000 || oldType == 1000) {
            // 新增或者去除超级管理员需要删除缓存
            String cacheKey = Constants.Account.SUPER_ADMIN_UID_LIST_CACHE.getCode();
            redisUtils.del(cacheKey);
        }
    }
    if (updateUserInfo && setNewPwd) {
        // 需要重新登录
        userRoleEntityService.deleteCache(uid, true);
    } else if (changeUserRole) {
        // 需要重新授权
        userRoleEntityService.deleteCache(uid, false);
    }
    if (changeUserRole) {
        // 获取当前登录的用户
        Session session = SecurityUtils.getSubject().getSession();
        UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
        String title = "权限变更通知(Authority Change Notice)";
        String content = userRoleEntityService.getAuthChangeContent(oldType, type);
        adminNoticeManager.addSingleNoticeToUser(userRolesVo.getUid(), uid, title, content, "Sys");
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UserRole(top.hcode.hoj.pojo.entity.user.UserRole) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) Session(org.apache.shiro.session.Session)

Aggregations

UserInfo (top.hcode.hoj.pojo.entity.user.UserInfo)6 UserRole (top.hcode.hoj.pojo.entity.user.UserRole)6 Transactional (org.springframework.transaction.annotation.Transactional)5 UserRecord (top.hcode.hoj.pojo.entity.user.UserRecord)4 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)3 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)3 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)3 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)2 LinkedList (java.util.LinkedList)2 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)2 HashMap (java.util.HashMap)1 HttpSession (javax.servlet.http.HttpSession)1 Session (org.apache.shiro.session.Session)1