larvituser 0.18.1 → 0.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,180 +0,0 @@
1
- 'use strict';
2
-
3
- const userLib = require('larvituser'),
4
- async = require('async');
5
-
6
- exports.run = function (req, res, cb) {
7
- const tasks = [],
8
- data = {'global': res.globalData};
9
-
10
- data.global.errors = [];
11
-
12
- // Make sure the user have the correct rights
13
- // This is set in larvitadmingui controllerGlobal
14
- if ( ! res.adminRights) {
15
- cb(new Error('Invalid rights'), req, res, {});
16
- return;
17
- }
18
-
19
- data.global.menuControllerName = 'adminUsers';
20
-
21
- if (data.global.formFields.save !== undefined) {
22
- const userFields = {};
23
-
24
- let newPassword,
25
- user;
26
-
27
- if (data.global.formFields.disableLogin === 'yes') {
28
- newPassword = false;
29
- } else {
30
- newPassword = data.global.formFields.password.trim();
31
- }
32
-
33
- // Format userFields
34
- for (let i = 0; data.global.formFields.fieldName[i] !== undefined; i ++) {
35
- const fieldName = data.global.formFields.fieldName[i],
36
- fieldValue = data.global.formFields.fieldValue[i];
37
-
38
- if (fieldValue !== '' && fieldValue !== undefined) {
39
- if (userFields[fieldName] === undefined) {
40
- userFields[fieldName] = [];
41
- }
42
-
43
- userFields[fieldName].push(fieldValue);
44
- }
45
- }
46
-
47
- // Create user if it did not exist
48
- if (data.global.urlParsed.query.uuid === undefined) {
49
-
50
- // Check so username is not taken
51
- tasks.push(function (cb) {
52
- userLib.usernameAvailable(data.global.formFields.username.trim(), function (err, result) {
53
- if (err) return cb(err);
54
-
55
- if (result !== true) {
56
- const err = new Error('Username is taken by another user');
57
- data.global.errors.push(err.message);
58
- return cb(err);
59
- }
60
-
61
- cb(err);
62
- });
63
- });
64
-
65
- // Create the user
66
- tasks.push(function (cb) {
67
- if (data.global.errors.length) return cb();
68
-
69
- userLib.create(data.global.formFields.username, newPassword, userFields, function (err, result) {
70
- user = result;
71
- cb(err);
72
- });
73
- });
74
- } else {
75
- tasks.push(function (cb) {
76
- userLib.fromUuid(data.global.urlParsed.query.uuid, function (err, result) {
77
- user = result;
78
- cb(err);
79
- });
80
- });
81
- }
82
-
83
- // Update username
84
- tasks.push(function (cb) {
85
- if ( ! user) {
86
- throw new Error('No user set');
87
- }
88
-
89
- data.global.formFields.username = data.global.formFields.username.trim();
90
-
91
- if (data.global.formFields.username === user.username) return cb();
92
-
93
- userLib.usernameAvailable(data.global.formFields.username, function (err, result) {
94
- if (err) return cb(err);
95
-
96
- if (result !== true) {
97
- data.global.errors.push('Username is taken by another user');
98
- }
99
-
100
- cb(err);
101
- });
102
- });
103
-
104
- tasks.push(function (cb) {
105
- if (data.global.formFields.username === user.username) return cb();
106
-
107
- if (data.global.errors.length) return cb();
108
-
109
- user.setUsername(data.global.formFields.username.trim(), cb);
110
- });
111
-
112
- // Update password
113
- if (newPassword !== '' || newPassword === false) {
114
- tasks.push(function (cb) {
115
- if (data.global.errors.length) return cb();
116
-
117
- user.setPassword(newPassword, cb);
118
- });
119
- }
120
-
121
- // Replace user fields
122
- tasks.push(function (cb) {
123
- if (data.global.errors.length) return cb();
124
-
125
- user.replaceFields(userFields, cb);
126
- });
127
-
128
- tasks.push(function (cb) {
129
- if (data.global.errors.length) return cb();
130
-
131
- if (data.global.urlParsed.query.uuid === undefined) {
132
- req.session.data.nextCallData = {'global': {'messages': ['New user created']}};
133
- res.statusCode = 302;
134
- res.setHeader('Location', '/adminUsers/edit?uuid=' + user.uuid);
135
- } else {
136
- data.global.messages = ['Saved'];
137
- }
138
-
139
- cb();
140
- });
141
- }
142
-
143
- if (data.global.formFields.rmUser !== undefined) {
144
- tasks.push(function (cb) {
145
- userLib.rmUser(data.global.urlParsed.query.uuid, function (err) {
146
- if (err) return cb(err);
147
-
148
- req.session.data.nextCallData = {'global': {'messages': ['User "' + data.global.urlParsed.query.uuid + '" erased']}};
149
- res.statusCode = 302;
150
- res.setHeader('Location', '/adminUsers/list');
151
- cb();
152
- });
153
- });
154
- }
155
-
156
- if (data.global.urlParsed.query.uuid !== undefined) {
157
- tasks.push(function (cb) {
158
- userLib.fromUuid(data.global.urlParsed.query.uuid, function (err, user) {
159
- if (err) return cb(err);
160
-
161
- data.user = {
162
- 'uuid': user.uuid,
163
- 'username': user.username,
164
- 'passwordIsFalse': user.passwordIsFalse,
165
- 'fields': user.fields
166
- };
167
-
168
- cb();
169
- });
170
- });
171
- }
172
-
173
- async.series(tasks, function (err) {
174
- if (err && ! data.global.errors) {
175
- cb(err, req, res, data);
176
- } else {
177
- cb(null, req, res, data);
178
- }
179
- });
180
- };
@@ -1,47 +0,0 @@
1
- 'use strict';
2
-
3
- const userLib = require(__dirname + '/../../index.js'),
4
- async = require('async');
5
-
6
- exports.run = function (req, res, cb) {
7
- const tasks = [],
8
- data = {'global': res.globalData};
9
-
10
- // Make sure the user have the correct rights
11
- // This is set in larvitadmingui controllerGlobal
12
- if ( ! res.adminRights) {
13
- cb(new Error('Invalid rights'), req, res, {});
14
- return;
15
- }
16
-
17
- data.global.menuControllerName = 'adminUsers';
18
- data.pagination = {};
19
- data.pagination.urlParsed = data.global.urlParsed;
20
- data.pagination.elementsPerPage = 100;
21
-
22
- tasks.push(function (cb) {
23
- const users = new userLib.Users();
24
-
25
- //users.returnFields = ['firstname', 'lastname'];
26
- users.limit = data.pagination.elementsPerPage;
27
- users.offset = parseInt(data.global.urlParsed.query.offset) || 0;
28
-
29
- if (isNaN(users.offset) || users.offset < 0) {
30
- users.offset = 0;
31
- }
32
-
33
- //if (data.global.urlParsed.query.filterStatus) {
34
- // users.matchAllFields = {'status': data.global.urlParsed.query.filterStatus};
35
- //}
36
-
37
- users.get(function (err, result, totalElements) {
38
- data.users = result;
39
- data.pagination.totalElements = totalElements;
40
- cb(err);
41
- });
42
- });
43
-
44
- async.series(tasks, function (err) {
45
- cb(err, req, res, data);
46
- });
47
- };