Commit 0b804dd235d4a19ab56e3081b92f05f3f2b8d558
1 parent
f8e4ee5571
Exists in
master
add and delete capability
Showing 5 changed files with 78 additions and 53 deletions Side-by-side Diff
index.js
... | ... | @@ -323,6 +323,9 @@ app.get('/users/:capability/delete-confirmation/:user_id/:user_email/', authNeed |
323 | 323 | app.get('/users/:capability/delete/:user_id/:value_to_delete', authNeeded, adminNeeded, usersController.deleteCapability); |
324 | 324 | app.get('/users/:capability/delete/:user_id/', authNeeded, adminNeeded, usersController.deleteCapability); |
325 | 325 | |
326 | +app.get('/users/:capability/add/:user_id/:user_email', authNeeded, adminNeeded, usersController.addCapabilityForm); | |
327 | +app.post('/users/:capability/add/:user_id/:user_email', authNeeded, adminNeeded, usersController.addCapability); | |
328 | + | |
326 | 329 | |
327 | 330 | app.listen(config.listen_port, function () { |
328 | 331 | console.log('Example app listening on port ' + config.listen_port); |
usersController.js
... | ... | @@ -6,6 +6,12 @@ var Controller = require('./controller.js'); |
6 | 6 | var UsersController = function(config) { |
7 | 7 | Controller.call(this, config); |
8 | 8 | |
9 | + function uniq(a) { | |
10 | + return a.sort().filter(function(item, pos, ary) { | |
11 | + return !pos || item != ary[pos - 1]; | |
12 | + }) | |
13 | + } | |
14 | + | |
9 | 15 | function index(req, res, next) { |
10 | 16 | this.mongodb.collection('users').find({}).toArray(function(err, docs) { |
11 | 17 | if (err) { |
... | ... | @@ -115,72 +121,56 @@ var UsersController = function(config) { |
115 | 121 | }); |
116 | 122 | } |
117 | 123 | |
118 | - function deleteRoleConfirmation(req, res, next) { | |
119 | - if (!req.params.role) { | |
120 | - req.params.role = ''; | |
121 | - } | |
122 | - | |
124 | + function addCapabilityForm(req, res, next) { | |
123 | 125 | res.render( |
124 | - 'users.roles.delete-confirmation.html', | |
126 | + 'users.capability.add.html', | |
125 | 127 | { |
126 | - title: 'Role delete confirmation', | |
128 | + title: 'Add a capability', | |
127 | 129 | session: req.session, |
128 | 130 | user_id: req.params.user_id, |
129 | 131 | user_email: req.params.user_email, |
130 | - user_role: req.params.role | |
132 | + capability: req.params.capability | |
131 | 133 | } |
132 | 134 | ); |
133 | 135 | } |
134 | 136 | |
135 | - function deleteSupplierConfirmation(req, res, next) { | |
136 | - if (!req.params.role) { | |
137 | - req.params.role = ''; | |
138 | - } | |
137 | + function addCapability(req, res, next) { | |
139 | 138 | |
140 | - res.render( | |
141 | - 'users.suppliers.delete-confirmation.html', | |
142 | - { | |
143 | - title: 'Supplier delete confirmation', | |
144 | - session: req.session, | |
145 | - user_id: req.params.user_id, | |
146 | - user_email: req.params.user_email, | |
147 | - user_supplier: req.params.supplier | |
148 | - } | |
149 | - ); | |
150 | - } | |
139 | + var values = uniq(req.body.new_capability.trim().split(/\W+/)); | |
151 | 140 | |
152 | - function deleteRole(req, res, next) { | |
153 | - | |
154 | - if (!req.params.role) { | |
155 | - req.params.role = ''; | |
141 | + var capabilityToPullBefore = {}; | |
142 | + capabilityToPullBefore[req.params.capability] = { | |
143 | + $in: values | |
156 | 144 | } |
157 | 145 | |
146 | + var capabilityToPush = {}; | |
147 | + capabilityToPush[req.params.capability] = { | |
148 | + $each: values, | |
149 | + $sort: 1 | |
150 | + }; | |
151 | + | |
158 | 152 | this.mongodb.collection('users').update( |
159 | - { _id: new mongoObjectID(req.params.user_id) }, | |
160 | - { $pull: {roles: req.params.role} }, | |
153 | + { _id: new mongoObjectID(req.body.user_id) }, | |
154 | + { $pull: capabilityToPullBefore }, | |
155 | + | |
161 | 156 | function(err, r) { |
162 | - if (err) { | |
163 | - res.send(err); return; | |
164 | - } | |
165 | - res.redirect('/users/view/' + req.params.user_id); | |
166 | - }); | |
167 | - } | |
168 | 157 | |
169 | - function deleteSupplier(req, res, next) { | |
158 | + if (err) { res.send(err); return; } | |
170 | 159 | |
171 | - if (!req.params.supplier) { | |
172 | - req.params.supplier = ''; | |
173 | - } | |
160 | + this.mongodb.collection('users').update( | |
161 | + { _id: new mongoObjectID(req.body.user_id) }, | |
162 | + { $push: capabilityToPush }, | |
174 | 163 | |
175 | - this.mongodb.collection('users').update( | |
176 | - { _id: new mongoObjectID(req.params.user_id) }, | |
177 | - { $pull: {suppliers: req.params.supplier} }, | |
178 | - function(err, r) { | |
179 | - if (err) { | |
180 | - res.send(err); return; | |
181 | - } | |
182 | - res.redirect('/users/view/' + req.params.user_id); | |
183 | - }); | |
164 | + function(err, r) { | |
165 | + | |
166 | + if (err) { res.send(err); return; } | |
167 | + | |
168 | + res.redirect('/users/view/' + req.params.user_id); | |
169 | + } | |
170 | + | |
171 | + ); | |
172 | + } | |
173 | + ); | |
184 | 174 | } |
185 | 175 | |
186 | 176 | return { |
... | ... | @@ -189,7 +179,9 @@ var UsersController = function(config) { |
189 | 179 | addForm: addForm, |
190 | 180 | addSave: addSave, |
191 | 181 | deleteCapability: deleteCapability, |
192 | - deleteCapabilityConfirmation: deleteCapabilityConfirmation | |
182 | + deleteCapabilityConfirmation: deleteCapabilityConfirmation, | |
183 | + addCapabilityForm: addCapabilityForm, | |
184 | + addCapability: addCapability | |
193 | 185 | } |
194 | 186 | } |
195 | 187 |
views/users.capability.add.html
... | ... | @@ -0,0 +1,22 @@ |
1 | +{% extends "starter-template.html" %} | |
2 | +{% block content %} | |
3 | + | |
4 | +<form class="form-horizontal" method="POST"> | |
5 | + | |
6 | + <input name="user_id" type="hidden" value="{{ user_id }}"> | |
7 | + | |
8 | + <div class="form-group"> | |
9 | + <label for="inputCapability" class="col-sm-2 control-label">New value for {{ user_email }} {{ capability }}</label> | |
10 | + <div class="col-sm-10"> | |
11 | + <input name="new_capability" type="text" class="form-control" id="inputCapability"> | |
12 | + </div> | |
13 | + </div> | |
14 | + | |
15 | + <div class="form-group"> | |
16 | + <div class="col-sm-offset-2 col-sm-10"> | |
17 | + <input type="submit" class="btn btn-primary"> | |
18 | + </div> | |
19 | + </div> | |
20 | + | |
21 | +</form> | |
22 | +{% endblock %} |
views/users.capability.delete-confirmation.html
1 | 1 | {% extends "starter-template.html" %} |
2 | 2 | {% block content %} |
3 | 3 | |
4 | -Are you sure to delete "{{ user_role }}" from {{ user_email }} {{ capability }}? | |
4 | +Are you sure to delete "{{ value_to_delete }}" from {{ user_email }} {{ capability }}? | |
5 | 5 | |
6 | 6 | <br><br> |
7 | 7 | <a href="/users/{{ capability }}/delete/{{ user_id }}/{{ value_to_delete }}" class="btn btn-danger" role="button">Yes, delete it!<a> |
views/users.view.html
... | ... | @@ -23,10 +23,15 @@ |
23 | 23 | <label for="inputSuppliers" class="col-sm-2 control-label">Suppliers</label> |
24 | 24 | <div class="col-sm-10"> |
25 | 25 | {% for supplier in user.suppliers %} |
26 | - <a href="/users/suppliers/delete-confirmation/{{ user._id }}/{{ user.email }}/{{ supplier }}" class="btn btn-default">{{ supplier }}</a> | |
26 | + <a href="/users/suppliers/delete-confirmation/{{ user._id }}/{{ user.email }}/{{ supplier }}" class="btn btn-default"> | |
27 | + | |
28 | + {{ supplier }} | |
29 | + <span class="glyphicon glyphicon-remove-circle glyphicon-align-right" aria-hidden="true"></span> | |
30 | + | |
31 | + </a> | |
27 | 32 | {% endfor %} |
28 | 33 | |
29 | - <a href="/users/suppliers/add/{{ user._id }}" class="btn btn-primary" role="button">Add a supplier</a> | |
34 | + <a href="/users/suppliers/add/{{ user._id }}/{{ user.email }}" class="btn btn-primary" role="button">Add a supplier</a> | |
30 | 35 | |
31 | 36 | </div> |
32 | 37 | </div> |
... | ... | @@ -35,7 +40,10 @@ |
35 | 40 | <label for="inputRoles" class="col-sm-2 control-label">Roles</label> |
36 | 41 | <div class="col-sm-10"> |
37 | 42 | {% for role in user.roles %} |
38 | - <a href="/users/roles/delete-confirmation/{{ user._id }}/{{ user.email }}/{{ role }}" class="btn btn-default">{{ role }}</a> | |
43 | + <a href="/users/roles/delete-confirmation/{{ user._id }}/{{ user.email }}/{{ role }}" class="btn btn-default"> | |
44 | + {{ role }} | |
45 | + <span class="glyphicon glyphicon-remove-circle glyphicon-align-right" aria-hidden="true"></span> | |
46 | + </a> | |
39 | 47 | {% endfor %} |
40 | 48 | |
41 | 49 | <a href="/users/roles/add/{{ user._id }}/{{ user.email }}" class="btn btn-primary" role="button">Add a role</a> |