Skip to content

Instantly share code, notes, and snippets.

@morenoh149
Last active June 2, 2017 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morenoh149/cf880d7cda1c75547968 to your computer and use it in GitHub Desktop.
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.
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();
/*
* 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();
@VinayaSathyanarayana
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment