Last active
June 2, 2017 11:04
-
-
Save morenoh149/cf880d7cda1c75547968 to your computer and use it in GitHub Desktop.
Demonstration of User Models. Keystone can display the adminUI using a boolean or virtual field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var keystone = require('keystone'), | |
Types = keystone.Field.Types; | |
/** | |
* canAccessKeystone using a Types.Boolean | |
* | |
* To set admin privileges change value of `user.canAccessKeystone` | |
* ========== | |
*/ | |
var User = new keystone.List('User'); | |
User.add({ | |
name: { type: Types.Name, required: true, index: true }, | |
email: { type: Types.Email, required: true, initial: true, index: true }, | |
password: { type: Types.Password, required: true, initial: true }, | |
canAccessKeystone: { type: Types.Boolean, required: true, initial: true} | |
); | |
/** | |
* Registration | |
*/ | |
User.defaultColumns = 'name, email, isAdmin'; | |
User.register(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* show AdminUI only if the user is an admin | |
* or, the user is a moderator who has paid money | |
*/ | |
var keystone = require('keystone'), | |
Types = keystone.Field.Types; | |
var User = new keystone.List('User'); | |
User.add({ | |
name: { type: Types.Name, required: true, index: true }, | |
email: { type: Types.Email, required: true, initial: true, index: true }, | |
password: { type: Types.Password, required: true, initial: true }, | |
paidBalance: { type: Types.Boolean }, | |
moderator: { type: Types.Boolean } | |
}, 'Permissions', { | |
isAdmin: { type: Boolean, label: 'Can access Keystone', index: true } | |
}); | |
// Provide access to Keystone | |
User.schema.virtual('canAccessKeystone').get(function() { | |
if (this.isAdmin) { | |
return true; | |
} else if (this.paidBalance && this.moderator){ | |
return true; | |
} else { | |
return false; | |
} | |
}); | |
/** | |
* Registration | |
*/ | |
User.defaultColumns = 'name, email, isAdmin'; | |
User.register(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If the user is "this.isAdmin" I would like to show the "User" List in Admin UI,
If not, the "User" list should not be shown.
==> Only the Admin should have access to User List
How should one implement that?