JOE
2018-10-23 7cce65fb63b3c2e59a90b82e6d2dda2cae76880b
add signup&account management
9 files modified
1 files deleted
245 ■■■■ changed files
model/account.js 7 ●●●●● patch | view | raw | blame | history
routes/accounts.js 81 ●●●●● patch | view | raw | blame | history
routes/modules.js 14 ●●●●● patch | view | raw | blame | history
views/accounts/create.html 35 ●●●● patch | view | raw | blame | history
views/accounts/list.html 26 ●●●●● patch | view | raw | blame | history
views/accounts/update.html 26 ●●●● patch | view | raw | blame | history
views/login.html 4 ●●●● patch | view | raw | blame | history
views/modules/update.html 2 ●●● patch | view | raw | blame | history
views/roles/update.html 2 ●●● patch | view | raw | blame | history
views/signup.html 48 ●●●●● patch | view | raw | blame | history
model/account.js
....@@ -2,12 +2,13 @@
22 const Schema = mongoose.Schema;
33
44 const AccountSchema = new Schema({
5
- login_id: {type: String, required: true, index: true, unique: true },
6
- name: {type: String, required: true},
7
- passwd: {type: String, required: true, bcrypt: true},
5
+ login_id: {type: String, required: true, index: true, unique: true},
6
+ name: {type: String, required: true, unique: true},
7
+ passwd: {type: String, required: true, bcrypt: true},
88 mobile: String,
99 organization: String,
1010 position: String,
11
+ account_type: String,
1112 role: {type: mongoose.Schema.ObjectId, ref: 'roles'},
1213 accessibles : [{type: mongoose.Schema.ObjectId, ref: 'modules'}],
1314 });
routes/accounts.js
....@@ -8,46 +8,77 @@
88 const util = require('../util/util.js');
99
1010 const Account = require('../model/account');
11
-
12
-router.post('/', function(req, res, next) {
13
- const data = Object.assign(req.body) || {};
14
- let hash = bcrypt.hashSync(data.passwd, 10);
15
- data.passwd = hash;
16
-
17
- Account.create(data).then(account => {
18
- res.json(account);
19
- }).catch(err => {
20
- util.reportError(err, res);
21
- });
22
-});
11
+const Modules = require('../model/module');
12
+const Roles = require('../model/role');
2313
2414 router.get('/', function(req, res, next) {
25
- Account.find({}).then(function (accounts) {
26
- res.json(accounts);
15
+ Account.find({}).populate('role', 'name').
16
+ exec(function (err, accounts) {
17
+ // param
18
+ const param = util.initParam();
19
+ param.accounts = accounts;
20
+ res.render('accounts/list', param);
21
+ });
22
+});
23
+
24
+router.get('/create', function(req, res, next) {
25
+ Account.find({}).then(async function (accounts) {
26
+ const param = util.initParam();
27
+ param.accounts = accounts;
28
+ param.roles = await Roles.find({}).exec();
29
+ param.modules = await Modules.find({}).exec();
30
+ res.render('accounts/create', param);
2731 }).catch(err => {
2832 util.reportError(err, res);
2933 });
3034 });
35
+
36
+router.post('/create', function(req, res, next) {
37
+ const data = Object.assign(req.body) || {};
38
+ Account.create(data).then(role => {
39
+ res.redirect('/accounts/');
40
+ }).catch(err => {
41
+ Modules.find({}).then(async function (modules) {
42
+ const param = util.initParam();
43
+ param.error = util.errorJson(err).result_msg;
44
+ param.roles = await Roles.find({}).exec();
45
+ param.modules = await Modules.find({}).exec();
46
+ param.data = data;
47
+ res.render('accounts/create', param);
48
+ }).catch(err => {
49
+ util.reportError(err, res);
50
+ });
51
+ });
52
+});
53
+
3154
3255 router.get('/:account_id', function(req, res, next) {
33
- Account.findOne({_id: req.params.account_id}).then(function (account) {
34
- res.json(account);
56
+ Account.findOne({_id: req.params.account_id}).then(async function (account) {
57
+ const param = util.initParam();
58
+ param.roles = await Roles.find({}).exec();
59
+ param.modules = await Modules.find({}).exec();
60
+ param.account = account;
61
+ res.render('accounts/update', param);
3562 }).catch(err => {
3663 util.reportError(err, res);
3764 });
3865 });
3966
40
-router.put('/:account_id', function(req, res, next) {
67
+router.post('/:account_id', function(req, res, next) {
4168 const data = Object.assign(req.body) || {};
42
- Account.updateMany({_id: req.params.account_id}, { $set: data}, function(err, response) {
43
- err?util.reportError(err, res):res.json({'updated': response.n});
69
+ Account.updateOne({_id: req.body.id}, { $set: data}, async function(err, response) {
70
+ const param = util.initParam();
71
+ if(err) {
72
+ param.error = util.errorJson(err).result_msg;
73
+ param.roles = await Roles.find({}).exec();
74
+ param.modules = await Modules.find({}).exec();
75
+ param.account = data;
76
+ res.render('accounts/update', param);
77
+ } else {
78
+ res.redirect('/accounts/');
79
+ }
4480 });
4581 });
4682
47
-router.delete('/:account_id', function(req, res, next) {
48
- Account.deleteMany({ _id: req.params.account_id }, function (err, response) {
49
- err?util.reportError(err, res):res.json({'deleted': response.n});
50
- });
51
-});
5283
53
-module.exports = router;
84
+module.exports = router;
routes/modules.js
....@@ -79,18 +79,4 @@
7979 });
8080 });
8181
82
-
83
-// router.put('/:module_id', function(req, res, next) {
84
-// const data = Object.assign(req.body) || {};
85
-// Modules.updateMany({_id: req.params.module_id}, { $set: data}, function(err, response) {
86
-// err?util.reportError(err, res):res.json({'updated': response.n});
87
-// });
88
-// });
89
-
90
-// router.delete('/:module_id', function(req, res, next) {
91
-// Modules.deleteMany({ _id: req.params.module_id }, function (err, response) {
92
-// err?util.reportError(err, res):res.json({'deleted': response.n});
93
-// });
94
-// });
95
-
9682 module.exports = router;
views/accounts/create.html
....@@ -1,34 +1,37 @@
11 {{>layout/header-temp}}
22 <div class="content">
3
- <form class="form-signup" action="create" method="post">
3
+ <form class="form-signup" action="create" method="post">
44 <h1>Add Account</h1>
5
+ {{#error}}
6
+ <h4 class="error">{{error}}</h4>
7
+ {{/error}}
58 <div>
69 <label for="name">Name :</label>
7
- <input type="text" name="name" id="name" class="form-control" placeholder="Name" required/>
10
+ <input type="text" name="name" id="name" class="form-control" placeholder="Name" value="{{data.name}}" required/>
811 </div>
912 <div>
10
- <label for="loginId">Email :</label>
11
- <input type="text" name="loginId" id="loginId" class="form-control" placeholder="Email" required/>
13
+ <label for="login_id">Email :</label>
14
+ <input type="text" name="login_id" id="login_id" class="form-control" placeholder="Email" value="{{data.login_id}}" required/>
1215 </div>
1316 <div>
14
- <label for="password">Password</label>
15
- <input type="password" name="password" id="password" placeholder="Password" required/>
17
+ <label for="passwd">Password</label>
18
+ <input type="password" name="passwd" id="passwd" placeholder="Password" value="{{data.passwd}}" required/>
1619 </div>
1720 <div>
1821 <label for="mobile">Mobile :</label>
19
- <input type="text" name="mobile" id="mobile" placeholder="Mobile" required/>
22
+ <input type="text" name="mobile" id="mobile" placeholder="Mobile" required value="{{data.mobile}}" />
2023 </div>
2124 <div>
2225 <label for="organization">Organization Name :</label>
23
- <input type="text" name="organization" id="organization" placeholder="Organization Name" required/>
26
+ <input type="text" name="organization" id="organization" placeholder="Organization Name" value="{{data.organization}}" required/>
2427 </div>
2528 <div>
2629 <label for="position">Position :</label>
27
- <input type="text" name="position" id="position" placeholder="Position" required/>
30
+ <input type="text" name="position" id="position" placeholder="Position" value="{{data.position}}" required/>
2831 </div>
2932 <div>
30
- <label for="accountType">Account Type :</label>
31
- <select name="accountType" id="accountType">
33
+ <label for="account_type">Account Type :</label>
34
+ <select name="account_type" id="account_type">
3235 <option value="mno" selected>MNO</option>
3336 <option value="pm">Publisher&Marketer</option>
3437 </select>
....@@ -47,13 +50,13 @@
4750 <div id="accessibles">
4851 {{#modules}}
4952 <div>
50
- {{#parentId}}
53
+ {{#parent_id}}
5154 <span class="empty-space">&nbsp;&nbsp;&nbsp;</span>
52
- {{/parentId}}
55
+ {{/parent_id}}
5356 <input type="checkbox" id="{{id}}" name="accessibles" value="{{id}}"
54
- {{#parentId}}
55
- data-parent-id="{{parentId}}"
56
- {{/parentId}}
57
+ {{#parent_id}}
58
+ data-parent-id="{{parent_id}}"
59
+ {{/parent_id}}
5760 />
5861 <label for="{{id}}">{{name}}</label>
5962 </div>
views/accounts/list.html
....@@ -5,9 +5,31 @@
55 {{#accounts}}
66 <h3>
77 [{{role.name}}] {{name}}
8
- <a href="{{contextRoot}}/accounts/update?id={{id}}"><span>edit</span></a>
9
- <a href="{{contextRoot}}/accounts/delete?id={{id}}"><span>delete</span></a>
8
+ <a href="/accounts/{{id}}"><span>edit</span></a>
9
+ <a id="{{id}}" class="btn_delete" href="#"><span>delete</span></a>
1010 </h3>
1111 {{/accounts}}
1212 </div>
13
+<script>
14
+ $(document).ready(function () {
15
+ $('.btn_delete').on('click', function(){
16
+ if(confirm("Are you sure you want to delete this?")){
17
+ const id = $(this).attr('id');
18
+ $.ajax({
19
+ url: "/api/accounts/" + id,
20
+ method:"delete"
21
+ }).done(function(data) {
22
+ if(data.deleted == 1) {
23
+ $('#'+id).parent().remove();
24
+ }
25
+ }).fail(function(error) {
26
+ alert(error);
27
+ });
28
+ }
29
+ else{
30
+ return false;
31
+ }
32
+ });
33
+ });
34
+</script>
1335 {{>layout/footer}}
views/accounts/update.html
....@@ -2,22 +2,22 @@
22 <div class="content">
33 <form action="{{contextRoot}}/accounts/update" method="post">
44 <h1>Modify Account</h1>
5
- {{#result}}
6
- <h4>{{result}}</h4>
7
- {{/result}}
5
+ {{#error}}
6
+ <h4 class="error">{{error}}</h4>
7
+ {{/error}}
88 {{#account}}
99 <div>
1010 <label for="name">Name :</label>
1111 <input type="text" name="name" id="name" placeholder="Name" required value="{{name}}"/>
12
- <input type="hidden" name="id" id="id" required value="{{id}}"/>
12
+ <input type="hidden" name="id" id="id" required value="{{id}}" readonly />
1313 </div>
1414 <div>
15
- <label for="loginId">Email :</label>
16
- <input type="text" name="loginId" id="loginId" placeholder="Email" required value="{{loginId}}"/>
15
+ <label for="login_id">Email :</label>
16
+ <input type="text" name="login_id" id="login_id" placeholder="Email" required value="{{login_id}}" readonly />
1717 </div>
1818 <div>
19
- <label for="password">Password</label>
20
- <input type="password" name="password" id="password" placeholder="Password" required value="{{password}}"/>
19
+ <label for="passwd">Password</label>
20
+ <input type="password" name="passwd" id="passwd" placeholder="Password" required value="{{passwd}}"/>
2121 </div>
2222 <div>
2323 <label for="mobile">Mobile :</label>
....@@ -34,7 +34,7 @@
3434 </div>
3535 <div>
3636 <label for="account_type">Account Type :</label>
37
- <select name="accountType" id="account_type">
37
+ <select name="account_type" id="account_type">
3838 <option value="mno" selected>MNO</option>
3939 <option value="pm">Publisher&Marketer</option>
4040 </select>
....@@ -68,7 +68,7 @@
6868 </fieldset>
6969 </div>
7070
71
- <button onClick="javasciprt:history.back();">Cancel</button>
71
+ <input type="button" onClick="javasciprt:history.back();" value="Cancel" />
7272 <button type="submit">Modify Module</button>
7373 </form>
7474 {{/account}}
....@@ -79,10 +79,10 @@
7979 </div>
8080 <script>
8181 $(function () {
82
- $('#account_type').val('{{account.accountType}}');
83
- $('#role').val('{{account.role.id}}');
82
+ $('#account_type').val('{{account.account_type}}');
83
+ $('#role').val('{{account.role}}');
8484 {{#account.accessibles}}
85
- $('#{{id}}').prop('checked', true);
85
+ $('#{{.}}').prop('checked', true);
8686 {{/account.accessibles}}
8787
8888 $('input[type=checkbox]').on('change', function () {
views/login.html
....@@ -4,7 +4,7 @@
44 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
55 <meta charset="utf-8">
66 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
7
- <title>MOBITHRU-Login</title>
7
+ <title>MOBITHRU</title>
88 <link href="{{cdnRoot}}/css/login.css" rel="stylesheet">
99
1010 <!-- Scripts -->
....@@ -47,7 +47,7 @@
4747 </form>
4848 <div class="divider-login-lcell"></div>
4949
50
- <a href="{{contextRoot}}/signup" class="btn-create-account" tabindex="4">Create Account</a>
50
+ <a href="{{contextRoot}}/accounts/create" class="btn-create-account" tabindex="4">Create Account</a>
5151 </div>
5252
5353 <div class="rcell">
views/modules/update.html
....@@ -33,7 +33,7 @@
3333 </select>
3434 </div>
3535
36
- <button onClick="javasciprt:history.back();">Cancel</button>
36
+ <input type="button" onClick="javasciprt:history.back();" value="Cancel" />
3737 <button type="submit">Modify Module</button>
3838 </form>
3939 {{/module}}
views/roles/update.html
....@@ -37,7 +37,7 @@
3737 </fieldset>
3838 </div>
3939
40
- <button onClick="javasciprt:history.back();">Cancel</button>
40
+ <input type="button" onClick="javasciprt:history.back();" value="Cancel" />
4141 <button type="submit">Modify Role</button>
4242 </form>
4343 {{/role}}
views/signup.html
deleted file mode 100644
....@@ -1,48 +0,0 @@
1
-{{>layout/header-temp}}
2
-<div class="content">
3
- <form class="form-signup" action="signup" method="post">
4
- <h1>Sign up</h1>
5
- <div>
6
- <label for="name">Name :</label>
7
- <input type="text" name="name" id="name" class="form-control" placeholder="Name" required/>
8
- </div>
9
- <div>
10
- <label for="login_id">Email :</label>
11
- <input type="text" name="login_id" id="login_id" class="form-control" placeholder="Email" required/>
12
- </div>
13
- <div>
14
- <label for="passwd">Password</label>
15
- <input type="password" name="passwd" id="passwd" placeholder="Password" required/>
16
- </div>
17
- <div>
18
- <label for="mobile">Mobile :</label>
19
- <input type="text" name="mobile" id="mobile" placeholder="Mobile" required/>
20
- </div>
21
- <div>
22
- <label for="organization">Organization Name :</label>
23
- <input type="text" name="organization" id="organization" placeholder="Organization Name" required/>
24
- </div>
25
- <div>
26
- <label for="position">Position :</label>
27
- <input type="text" name="position" id="position" placeholder="Position" required/>
28
- </div>
29
- <div>
30
- <label for="accountType">Account Type :</label>
31
- <select name="accountType" id="accountType">
32
- <option value="mno" selected>MNO</option>
33
- <option value="pm">Publisher&Marketer</option>
34
- </select>
35
- </div>
36
- <div>
37
- <label for="role">Role Type :</label>
38
- <select name="role" id="role">
39
- {{#roles}}
40
- <option value="{{id}}">{{name}}</option>
41
- {{/roles}}
42
- </select>
43
- </div>
44
-
45
- <button type="submit">Sign Up</button>
46
- </form>
47
-</div>
48
-{{>layout/footer}}