Skip to content

Commit

Permalink
Merge pull request #71 from Antabot/dev
Browse files Browse the repository at this point in the history
feat: bulk registration
  • Loading branch information
Antabot authored Dec 14, 2019
2 parents 944213b + aae1a30 commit e5efd45
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 15 deletions.
4 changes: 4 additions & 0 deletions wj-vue/src/components/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
_this.$store.commit('login', data)
var path = _this.$route.query.redirect
_this.$router.replace({path: path === '/' || path === undefined ? '/admin/dashboard' : path})
} else {
this.$alert(resp.data.message, '提示', {
confirmButtonText: '确定'
})
}
})
.catch(failResponse => {})
Expand Down
32 changes: 27 additions & 5 deletions wj-vue/src/components/Register.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
<template>
<body id="paper">
<el-form class="login-container" label-position="left"
<el-form :model="loginForm" :rules="rules" class="login-container" label-position="left"
label-width="0px" v-loading="loading">
<h3 class="login_title">用户注册</h3>
<el-form-item>
<el-form-item prop="username">
<el-input type="text" v-model="loginForm.username"
auto-complete="off" placeholder="账号"></el-input>
</el-form-item>
<el-form-item>
<el-form-item prop="password">
<el-input type="password" v-model="loginForm.password"
auto-complete="off" placeholder="密码"></el-input>
</el-form-item>
<el-form-item>
<el-input type="text" v-model="loginForm.name"
auto-complete="off" placeholder="真实姓名"></el-input>
</el-form-item>
<el-form-item>
<el-input type="text" v-model="loginForm.phone"
auto-complete="off" placeholder="电话号码"></el-input>
</el-form-item>
<el-form-item>
<el-input type="text" v-model="loginForm.email"
auto-complete="off" placeholder="E-Mail"></el-input>
</el-form-item>
<el-form-item style="width: 100%">
<el-button type="primary" style="width: 40%;background: #505458;border: none" v-on:click="register">注册</el-button>
</el-form-item>
Expand All @@ -21,10 +33,17 @@
export default{
data () {
return {
rules: {
username: [{required: true, message: '用户名不能为空', trigger: 'blur'}],
password: [{required: true, message: '密码不能为空', trigger: 'blur'}]
},
checked: true,
loginForm: {
username: '',
password: ''
password: '',
name: '',
phone: '',
email: ''
},
loading: false
}
Expand All @@ -35,7 +54,10 @@
this.$axios
.post('/register', {
username: this.loginForm.username,
password: this.loginForm.password
password: this.loginForm.password,
name: this.loginForm.name,
phone: this.loginForm.phone,
email: this.loginForm.email
})
.then(resp => {
if (resp.data.code === 200) {
Expand Down
102 changes: 102 additions & 0 deletions wj-vue/src/components/admin/user/BulkRegistration.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<template>
<div style="text-align: left">
<el-button class="add-button" type="success" @click="dialogFormVisible = true">批量添加用户</el-button>
<el-dialog
title="添加用户"
:visible.sync="dialogFormVisible"
@close="clear"
width="20%">
<el-form :model="loginForm" :rules="rules" label-position="left"
label-width="0px" v-loading="loading">
<el-form-item prop="username">
<el-input type="text" v-model="loginForm.username"
auto-complete="off" placeholder="账号"></el-input>
</el-form-item>
<el-form-item prop="password">
<el-input type="password" v-model="loginForm.password"
auto-complete="off" placeholder="密码"></el-input>
</el-form-item>
<el-form-item>
<el-input type="text" v-model="loginForm.name"
auto-complete="off" placeholder="真实姓名"></el-input>
</el-form-item>
<el-form-item>
<el-input type="text" v-model="loginForm.phone"
auto-complete="off" placeholder="电话号码"></el-input>
</el-form-item>
<el-form-item>
<el-input type="text" v-model="loginForm.email"
auto-complete="off" placeholder="E-Mail"></el-input>
</el-form-item>
<el-form-item style="width: 100%">
<el-button type="primary" style="width: 40%;background: #505458;border: none" v-on:click="register">添加</el-button>
</el-form-item>
</el-form>
</el-dialog>
</div>
</template>

<script>
export default {
name: 'BulkRegistration',
data () {
return {
dialogFormVisible: false,
rules: {
username: [{required: true, message: '用户名不能为空', trigger: 'blur'}],
password: [{required: true, message: '密码不能为空', trigger: 'blur'}]
},
checked: true,
loginForm: {
username: '',
password: '',
name: '',
phone: '',
email: ''
},
loading: false
}
},
methods: {
clear () {
this.loginForm = {
username: '',
password: '',
name: '',
phone: '',
email: ''
}
},
register () {
this.$axios
.post('/register', {
username: this.loginForm.username,
password: this.loginForm.password,
name: this.loginForm.name,
phone: this.loginForm.phone,
email: this.loginForm.email
})
.then(resp => {
if (resp.data.code === 200) {
this.$alert('注册成功', '提示', {
confirmButtonText: '确定'
})
this.clear()
this.$emit('onSubmit')
} else {
this.$alert(resp.data.message, '提示', {
confirmButtonText: '确定'
})
}
})
.catch(failResponse => {})
}
}
}
</script>

<style scoped>
.add-button {
margin: 18px 0 0 10px;
}
</style>
12 changes: 9 additions & 3 deletions wj-vue/src/components/admin/user/UserProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<el-form-item label="密码" label-width="120px" prop="password">
<el-button type="warning" @click="resetPassword(selectedUser.username)">重置密码</el-button>
</el-form-item>
<el-form-item label="角色" label-width="120px" prop="roles">
<el-form-item label="角色分配" label-width="120px" prop="roles">
<el-checkbox-group v-model="selectedRoles">
<el-checkbox v-for="(role,i) in roles" :key="i" :label="role.id">{{role.nameZh}}</el-checkbox>
</el-checkbox-group>
Expand All @@ -37,10 +37,12 @@
<el-breadcrumb-item>用户信息</el-breadcrumb-item>
</el-breadcrumb>
</el-row>
<bulk-registration @onSubmit="listUsers()"></bulk-registration>
<el-card style="margin: 18px 2%;width: 95%">
<el-table
:data="users"
stripe
:default-sort = "{prop: 'id', order: 'ascending'}"
style="width: 100%"
:max-height="tableHeight">
<el-table-column
Expand All @@ -50,6 +52,7 @@
<el-table-column
prop="id"
label="id"
sortable
width="100">
</el-table-column>
<el-table-column
Expand All @@ -75,6 +78,7 @@
</el-table-column>
<el-table-column
label="状态"
sortable
width="100">
<template slot-scope="scope">
<el-switch
Expand Down Expand Up @@ -113,8 +117,10 @@
</template>

<script>
import BulkRegistration from './BulkRegistration'
export default {
name: 'UserProfile',
name: 'UserProfile',
components: {BulkRegistration},
data () {
return {
users: [],
Expand Down Expand Up @@ -205,7 +211,7 @@
this.selectedRoles = roleIds
},
resetPassword (username) {
this.$axios.put('/password', {
this.$axios.put('/admin/password', {
username: username
}).then(resp => {
if (resp && resp.status === 200) {
Expand Down
1 change: 1 addition & 0 deletions wj/src/main/java/com/gm/wj/config/ShiroConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
customizedFilter.put("url", getURLPathMatchingFilter()); // 自定义过滤器设置 2,命名,需在设置过滤路径前

// filterChainDefinitionMap.put("/api/authentication", "authc"); // 防鸡贼登录,暂时不需要
filterChainDefinitionMap.put("/api/menu", "authc");
filterChainDefinitionMap.put("/api/admin/**", "authc");

filterChainDefinitionMap.put("/api/admin/**", "url"); // 自定义过滤器设置 3,设置过滤路径
Expand Down
31 changes: 26 additions & 5 deletions wj/src/main/java/com/gm/wj/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ public Result login(@RequestBody User requestUser) {
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, requestUser.getPassword());
usernamePasswordToken.setRememberMe(true);
try {
User user = userService.findByUserName(username);
if (!user.isEnabled()) {
String message = "该用户已被禁用";
return ResultFactory.buildFailResult(message);
}
subject.login(usernamePasswordToken);
// 生成随机 token 并存储在 session 中
User user = userService.findByUserName(username);
return ResultFactory.buildSuccessResult(usernamePasswordToken);

} catch (AuthenticationException e) {
String message = "账号密码错误";
String message = "账号或密码错误";
return ResultFactory.buildFailResult(message);
}
}
Expand All @@ -46,12 +51,28 @@ public Result login() {
return ResultFactory.buildSuccessResult(message);
}

@PostMapping("api/register")
@PostMapping("/api/register")
public Result register(@RequestBody User user) {
String username = user.getUsername();
String name = user.getName();
String phone = user.getPhone();
String email = user.getEmail();
String password = user.getPassword();

username = HtmlUtils.htmlEscape(username);
user.setUsername(username);
name = HtmlUtils.htmlEscape(name);
user.setName(name);
phone = HtmlUtils.htmlEscape(phone);
user.setPhone(phone);
email = HtmlUtils.htmlEscape(email);
user.setEmail(email);
user.setEnabled(true);

if (username.equals("") || password.equals("")) {
String message = "用户名或密码为空,注册失败";
return ResultFactory.buildFailResult(message);
}

boolean exist = userService.isExist(username);

Expand All @@ -72,7 +93,7 @@ public Result register(@RequestBody User user) {
return ResultFactory.buildSuccessResult(user);
}

@GetMapping("api/logout")
@GetMapping("/api/logout")
public Result logout() {
Subject subject = SecurityUtils.getSubject();
subject.logout();
Expand All @@ -81,7 +102,7 @@ public Result logout() {
}

@GetMapping(value = "api/authentication")
public String authentication(){
public String authentication() {
return "身份认证成功";
}
}
4 changes: 2 additions & 2 deletions wj/src/main/java/com/gm/wj/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Result updateUserStatus(@RequestBody User requestUser) {
return ResultFactory.buildSuccessResult(message);
}

@PutMapping("api/password")
@PutMapping("/api/admin/password")
public Result resetPassword(@RequestBody User requestUser) {
User user = userService.findByUserName(requestUser.getUsername());
String salt = new SecureRandomNumberGenerator().nextBytes().toString();
Expand All @@ -58,7 +58,7 @@ public Result resetPassword(@RequestBody User requestUser) {
return ResultFactory.buildSuccessResult(message);
}

@PutMapping("api/admin/user")
@PutMapping("/api/admin/user")
public Result editUser(@RequestBody User requestUser) {
User user = userService.findByUserName(requestUser.getUsername());
user.setName(requestUser.getName());
Expand Down

0 comments on commit e5efd45

Please sign in to comment.