-
Notifications
You must be signed in to change notification settings - Fork 3
/
AdminProfileCollection.js
110 lines (102 loc) · 3.64 KB
/
AdminProfileCollection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { Meteor } from 'meteor/meteor';
import SimpleSchema from 'simpl-schema';
import BaseProfileCollection from './BaseProfileCollection';
import { ROLE } from '../role/Role';
import { Users } from './UserCollection';
class AdminProfileCollection extends BaseProfileCollection {
constructor() {
super('AdminProfile', new SimpleSchema({}));
}
/**
* Defines the profile associated with an Admin and the associated Meteor account.
* @param email The email associated with this profile. Will be the username.
* @param password The password for this user.
* @param firstName The first name.
* @param lastName The last name.
*/
define({ email, firstName, lastName, password }) {
if (Meteor.isServer) {
// console.log('define', email, firstName, lastName, password);
const username = email;
const user = this.findOne({ email, firstName, lastName });
if (!user) {
const role = ROLE.ADMIN;
const profileID = this._collection.insert({ email, firstName, lastName, userID: this.getFakeUserId(), role });
const userID = Users.define({ username, role, password });
this._collection.update(profileID, { $set: { userID } });
return profileID;
}
return user._id;
}
return undefined;
}
/**
* Updates the AdminProfile. You cannot change the email or role.
* @param docID the id of the AdminProfile
* @param firstName new first name (optional).
* @param lastName new last name (optional).
*/
update(docID, { firstName, lastName }) {
this.assertDefined(docID);
const updateData = {};
if (firstName) {
updateData.firstName = firstName;
}
if (lastName) {
updateData.lastName = lastName;
}
this._collection.update(docID, { $set: updateData });
}
/**
* Removes this profile, given its profile ID.
* Also removes this user from Meteor Accounts.
* @param profileID The ID for this profile object.
*/
removeIt(profileID) {
if (this.isDefined(profileID)) {
return super.removeIt(profileID);
}
return null;
}
/**
* Implementation of assertValidRoleForMethod. Asserts that userId is logged in as an Admin or Admin.
* This is used in the define, update, and removeIt Meteor methods associated with each class.
* @param userId The userId of the logged in user. Can be null or undefined
* @throws { Meteor.Error } If there is no logged in user, or the user is not an Admin or Admin.
*/
assertValidRoleForMethod(userId) {
this.assertRole(userId, [ROLE.ADMIN]);
}
/**
* Returns an array of strings, each one representing an integrity problem with this collection.
* Returns an empty array if no problems were found.
* Checks the profile common fields and the role..
* @returns {Array} A (possibly empty) array of strings indicating integrity issues.
*/
checkIntegrity() {
const problems = [];
this.find().forEach((doc) => {
if (doc.role !== ROLE.ADMIN) {
problems.push(`AdminProfile instance does not have ROLE.ADMIN: ${doc}`);
}
});
return problems;
}
/**
* Returns an object representing the AdminProfile docID in a format acceptable to define().
* @param docID The docID of a AdminProfile
* @returns { Object } An object representing the definition of docID.
*/
dumpOne(docID) {
const doc = this.findDoc(docID);
const email = doc.email;
const firstName = doc.firstName;
const lastName = doc.lastName;
return { email, firstName, lastName };
}
}
/**
* Profides the singleton instance of this class to all other entities.
* @type {AdminProfileCollection}
*/
export const AdminProfiles = new AdminProfileCollection();