Guest User

Untitled

a guest
Jun 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. function(newDoc, oldDoc, userCtx) {
  2.  
  3. if (newDoc._deleted === true) {
  4. // allow deletes by 'cluster admins', 'system admins' and matching users
  5. // without checking the other fields
  6. if ((userCtx.roles.indexOf('_admin') !== -1) || (userCtx.roles.indexOf('system_admin') !== -1) || (userCtx.name == oldDoc.name)) {
  7. return;
  8. } else {
  9. throw({forbidden: 'Only admins may delete other user docs. **************'});
  10. }
  11. }
  12.  
  13. if ((oldDoc && oldDoc.type !== 'user') || newDoc.type !== 'user') {
  14. throw({forbidden : 'doc.type must be user'});
  15. } // we only allow user docs for now
  16.  
  17. if (!newDoc.name) {
  18. throw({forbidden: 'doc.name is required'});
  19. }
  20.  
  21. if (!(newDoc.roles && (typeof newDoc.roles.length !== 'undefined'))) {
  22. throw({forbidden: 'doc.roles must be an array'});
  23. }
  24.  
  25. if (newDoc._id !== ('org.couchdb.user:' + newDoc.name)) {
  26. throw({
  27. forbidden: 'Doc ID must be of the form org.couchdb.user:name'
  28. });
  29. }
  30.  
  31. if (oldDoc) { // validate all updates
  32. if (oldDoc.name !== newDoc.name) {
  33. throw({forbidden: 'Usernames can not be changed.'});
  34. }
  35. }
  36.  
  37. if (newDoc.password_sha && !newDoc.salt) {
  38. throw({
  39. forbidden: 'Users with password_sha must have a salt.' +
  40. 'See /_utils/script/couch.js for example code.'
  41. });
  42. }
  43.  
  44. if (userCtx.roles.indexOf('_admin') === -1 && userCtx.roles.indexOf('system_admin') === -1 ) {
  45. if (oldDoc) { // validate non-admin updates
  46. if (userCtx.name !== newDoc.name) {
  47. throw({
  48. forbidden: 'You may only update your own user document.'
  49. });
  50. }
  51. // validate role updates
  52. var oldRoles = oldDoc.roles.sort();
  53. var newRoles = newDoc.roles.sort();
  54.  
  55. if (oldRoles.length !== newRoles.length) {
  56. throw({forbidden: 'Only _admin may edit roles'});
  57. }
  58.  
  59. for (var i = 0; i < oldRoles.length; i++) {
  60. if (oldRoles[i] !== newRoles[i]) {
  61. throw({forbidden: 'Only _admin may edit roles'});
  62. }
  63. }
  64. } else if (newDoc.roles.length > 0) {
  65. throw({forbidden: 'Only _admin may set roles'});
  66. }
  67. }
  68.  
  69. // no system roles in users db
  70. for (var i = 0; i < newDoc.roles.length; i++) {
  71. if (newDoc.roles[i][0] === '_') {
  72. throw({
  73. forbidden:
  74. 'No system roles (starting with underscore) in users db.'
  75. });
  76. }
  77. }
  78.  
  79. // no system names as names
  80. if (newDoc.name[0] === '_') {
  81. throw({forbidden: 'Username may not start with underscore.'});
  82. }
  83. }
Add Comment
Please, Sign In to add comment