Vulcan/packages/vulcan-users/lib/schema.js

266 lines
6.7 KiB
JavaScript
Raw Normal View History

import SimpleSchema from 'simpl-schema';
import Users from './collection.js';
import { Utils } from 'meteor/vulcan:lib'; // import from vulcan:lib because vulcan:core isn't loaded yet
2016-06-23 15:00:58 +09:00
const adminGroup = {
name: "admin",
order: 10
};
const ownsOrIsAdmin = (user, document) => {
return Users.owns(user, document) || Users.isAdmin(user);
};
2016-06-23 15:00:58 +09:00
/**
* @summary Users schema
* @type {Object}
2016-06-23 15:00:58 +09:00
*/
const schema = {
_id: {
type: String,
optional: true,
viewableBy: ['guests'],
2016-06-23 15:00:58 +09:00
},
username: {
type: String,
optional: true,
viewableBy: ['guests'],
insertableBy: ['guests'],
onInsert: user => {
if (user.services.twitter && user.services.twitter.screenName) {
return user.services.twitter.screenName;
}
}
},
emails: {
type: Array,
optional: true,
},
'emails.$': {
type: Object,
optional: true,
2016-06-23 15:00:58 +09:00
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email,
optional: true,
},
"emails.$.verified": {
type: Boolean,
optional: true,
},
createdAt: {
type: Date,
optional: true,
viewableBy: ['guests'],
onInsert: () => {
return new Date();
}
},
isAdmin: {
type: Boolean,
label: "Admin",
control: "checkbox",
optional: true,
insertableBy: ['admins'],
editableBy: ['admins'],
viewableBy: ['guests'],
group: adminGroup,
onInsert: user => {
// if this is not a dummy account, and is the first user ever, make them an admin
const realUsersCount = Users.find({'isDummy': {$ne: true}}).count();
return (!user.isDummy && realUsersCount === 0) ? true : false;
}
},
profile: {
type: Object,
optional: true,
blackbox: true,
insertableBy: ['guests'],
},
// telescope-specific data, kept for backward compatibility and migration purposes
telescope: {
type: Object,
blackbox: true,
optional: true,
viewableBy: ['guests'],
},
services: {
type: Object,
optional: true,
blackbox: true,
},
2016-06-23 15:00:58 +09:00
/**
Bio (Markdown version)
*/
bio: {
2016-06-23 15:00:58 +09:00
type: String,
optional: true,
control: "textarea",
insertableBy: ['members'],
editableBy: ['members'],
viewableBy: ['guests'],
2016-06-23 15:00:58 +09:00
},
/**
The name displayed throughout the app. Can contain spaces and special characters, doesn't need to be unique
*/
displayName: {
2016-06-23 15:00:58 +09:00
type: String,
optional: true,
control: "text",
insertableBy: ['members'],
editableBy: ['members'],
viewableBy: ['guests'],
onInsert: (user, options) => {
2017-05-03 15:37:44 +09:00
const profileName = Utils.getNestedProperty(user, 'profile.name');
const twitterName = Utils.getNestedProperty(user, 'services.twitter.screenName');
const linkedinFirstName = Utils.getNestedProperty(user, 'services.linkedin.firstName');
if (profileName) return profileName;
if (twitterName) return twitterName;
if (linkedinFirstName) return `${linkedinFirstName} ${Utils.getNestedProperty(user, 'services.linkedin.lastName')}`;
if (user.username) return user.username;
return undefined;
}
2016-06-23 15:00:58 +09:00
},
/**
The user's email. Modifiable.
*/
email: {
2016-06-23 15:00:58 +09:00
type: String,
optional: true,
regEx: SimpleSchema.RegEx.Email,
mustComplete: true,
2016-06-23 15:00:58 +09:00
control: "text",
insertableBy: ['guests'],
editableBy: ['members'],
viewableBy: ownsOrIsAdmin,
onInsert: (user) => {
// look in a few places for the user email
const meteorEmails = Utils.getNestedProperty(user, 'services.meteor-developer.emails');
const facebookEmail = Utils.getNestedProperty(user, 'services.facebook.email');
const githubEmail = Utils.getNestedProperty(user, 'services.github.email');
const googleEmail = Utils.getNestedProperty(user, 'services.google.email');
const linkedinEmail = Utils.getNestedProperty(user, 'services.linkedin.emailAddress');
2017-05-03 15:37:44 +09:00
if (meteorEmails) return _.findWhere(meteorEmails, { primary: true }).address;
if (facebookEmail) return facebookEmail;
if (githubEmail) return githubEmail;
if (googleEmail) return googleEmail;
if (linkedinEmail) return linkedinEmail;
return undefined;
}
2016-06-23 15:00:58 +09:00
// unique: true // note: find a way to fix duplicate accounts before enabling this
},
/**
A hash of the email, used for Gravatar // TODO: change this when email changes
*/
emailHash: {
2016-06-23 15:00:58 +09:00
type: String,
optional: true,
viewableBy: ['guests'],
onInsert: user => {
if (user.email) {
return Users.avatar.hash(user.email);
}
}
2016-06-23 15:00:58 +09:00
},
avatarUrl: {
type: String,
optional: true,
viewableBy: ['guests'],
onInsert: user => {
2017-05-03 15:37:44 +09:00
2017-05-03 14:42:39 +09:00
const twitterAvatar = Utils.getNestedProperty(user, 'services.twitter.profile_image_url_https');
2017-05-03 15:37:44 +09:00
const facebookId = Utils.getNestedProperty(user, 'services.facebook.id');
2017-05-03 14:42:39 +09:00
if (twitterAvatar) return twitterAvatar;
2017-05-03 15:37:44 +09:00
if (facebookId) return `https://graph.facebook.com/${facebookId}/picture?type=large`;
return undefined;
}
},
2016-06-23 15:00:58 +09:00
/**
The HTML version of the bio field
*/
htmlBio: {
2016-06-23 15:00:58 +09:00
type: String,
optional: true,
viewableBy: ['guests'],
2016-06-23 15:00:58 +09:00
},
/**
The user's karma
*/
karma: {
2016-06-23 15:00:58 +09:00
type: Number,
optional: true,
viewableBy: ['guests'],
2016-06-23 15:00:58 +09:00
},
/**
The user's profile URL slug // TODO: change this when displayName changes
*/
slug: {
2016-06-23 15:00:58 +09:00
type: String,
optional: true,
viewableBy: ['guests'],
onInsert: user => {
// create a basic slug from display name and then modify it if this slugs already exists;
const basicSlug = Utils.slugify(user.displayName);
return Utils.getUnusedSlug(Users, basicSlug);
}
2016-06-23 15:00:58 +09:00
},
/**
The user's Twitter username
2016-06-23 15:00:58 +09:00
*/
twitterUsername: {
2016-06-23 15:00:58 +09:00
type: String,
optional: true,
control: "text",
insertableBy: ['members'],
editableBy: ['members'],
viewableBy: ['guests'],
resolveAs: 'twitterUsername: String',
onInsert: user => {
if (user.services && user.services.twitter && user.services.twitter.screenName) {
return user.services.twitter.screenName;
}
}
2016-06-23 15:00:58 +09:00
},
/**
A link to the user's homepage
*/
website: {
2016-06-23 15:00:58 +09:00
type: String,
regEx: SimpleSchema.RegEx.Url,
optional: true,
control: "text",
insertableBy: ['members'],
editableBy: ['members'],
viewableBy: ['guests'],
2016-07-19 17:30:59 +09:00
},
/**
Groups
*/
groups: {
type: Array,
2016-07-19 17:30:59 +09:00
optional: true,
control: "checkboxgroup",
insertableBy: ['admins'],
editableBy: ['admins'],
viewableBy: ['guests'],
form: {
2016-07-19 17:30:59 +09:00
options: function () {
const groups = _.without(_.keys(Users.groups), "guests", "members", "admins");
2016-07-19 17:30:59 +09:00
return groups.map(group => {return {value: group, label: group};});
}
},
},
'groups.$': {
type: String,
optional: true
}
};
export default schema;