Guest User

Untitled

a guest
Dec 21st, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. const userSchema = new Schema({
  2. first_name: String,
  3. last_name: String,
  4. email: String,
  5. phone: String,
  6. avatar: String,
  7. password: String,
  8. active: Boolean
  9. });
  10.  
  11. const User = mongoose.model('user', userSchema);
  12.  
  13. const accountSchema = mongoose.Schema({
  14. account_type: { type: String, enum: ['single', 'organization'], default: 'single' },
  15. organization: { type: Schema.Types.ObjectId, ref: 'organization', required: false },
  16. billing_address: String,
  17. shipping_address: String,
  18.  
  19. });
  20.  
  21. const Account = mongoose.model('account', accountSchema);
  22.  
  23. const accountUserSchema = mongoose.Schema({
  24. user : { type: Schema.Types.ObjectId, ref: 'user', },
  25. role: { type: String, enum: ['admin', 'user'], default: 'user' },
  26. account: { type: Schema.Types.ObjectId, ref: 'account', required: true }
  27. });
  28.  
  29. const AccountUser = mongoose.model('accountUser', accountUserRoleSchema);
  30.  
  31.  
  32. const permissionSchema = mongoose.Schema({
  33. user : { type: Schema.Types.ObjectId, ref: 'user', required: true },
  34. type: { type: Schema.Types.ObjectId, ref: 'permissionType', required: true },
  35. read: { type: Boolean, default: false, required: true },
  36. write: { type: Boolean, default: false, required: true },
  37. delete: { type: Boolean, default: false, required: true },
  38. accountUser : { type: Schema.Types.ObjectId, ref: 'account',required: true }
  39.  
  40. });
  41.  
  42. const Permission = mongoose.model('permission', permissionSchema);
  43.  
  44.  
  45. const permissionTypeSchema = mongoose.Schema({
  46. name : { type: String, required: true }
  47.  
  48. });
  49.  
  50. const PermissionType = mongoose.model('permissionType', permissionTypeSchema);
  51.  
  52.  
  53. const organizationSchema = mongoose.Schema({
  54. account : { type: Schema.Types.ObjectId, ref: 'account', },
  55. name: { type: String, required: true },
  56. logo: { type: String, required: true }
  57. });
  58.  
  59.  
  60. const Organization = mongoose.model('organization', organizationSchema);
  61.  
  62. module.exports = {
  63.  
  64. checkAccess : (permission_type,action) => {
  65.  
  66. return async (req, res, next) => {
  67.  
  68. // check if the user object is in the request after verifying jwt
  69. if(req.user){
  70.  
  71. // find the accountUserRole with the user data from the req after passort jwt auth
  72. const accountUser = AccountUserRole.findOne({ user :new ObjectId( req.user._id) }).populate('account','type');
  73. if(accountUser)
  74. {
  75. // find the account and check the type
  76.  
  77. if(accountUser.account)
  78. {
  79. if(accountUser.account.type === 'single')
  80. {
  81. // if account is single grant access
  82. return next();
  83. }
  84. else if(accountUser.account.type === 'organization'){
  85.  
  86.  
  87. // find the user permission
  88.  
  89. // check permission with permission type and see if action is true
  90.  
  91. // if true move to next middileware else throw access denied error
  92.  
  93.  
  94. }
  95. }
  96.  
  97. }
  98.  
  99. }
  100. }
  101.  
  102.  
  103. }
  104.  
  105.  
  106. }
  107.  
  108. const userSchema = new Schema({
  109. first_name: String,
  110. last_name: String,
  111. email: String,
  112. phone: String,
  113. avatar: String,
  114. password: String,
  115. active: Boolean
  116. account : { type: Schema.Types.ObjectId, ref: 'account', },
  117. role: { type: String, enum: ['admin', 'user'], default: 'user' },
  118. permssion: [
  119. {
  120. type: { type: Schema.Types.ObjectId, ref: 'permissionType', required: true },
  121. read: { type: Boolean, default: false, required: true },
  122. write: { type: Boolean, default: false, required: true },
  123. delete: { type: Boolean, default: false, required: true },
  124. }
  125. ]
  126.  
  127. });
  128.  
  129. const User = mongoose.model('user', userSchema);
  130.  
  131. const accountSchema = mongoose.Schema({
  132. account_type: { type: String, enum: ['single', 'organization'], default: 'single' },
  133. organization: {
  134. name: { type: String, required: true },
  135. logo: { type: String, required: true }
  136. },
  137. billing_address: String,
  138. shipping_address: String,
  139.  
  140. });
  141.  
  142.  
  143. const Account = mongoose.model('account', accountSchema);
  144.  
  145.  
  146. const permissionTypeSchema = mongoose.Schema({
  147. name : { type: String, required: true }
  148.  
  149. });
  150.  
  151. const PermissionType = mongoose.model('permissionType', permissionTypeSchema);
Add Comment
Please, Sign In to add comment