-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
162 lines (157 loc) · 5.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import cloneDeep from 'lodash/cloneDeep'
import { create, update, deleteEntity, fetch, query, fetchAll } from '../../entity'
/**
* A role is a collection of permissions that will be applicable to all the users who are assigned this role. Read more about <a href= 'https://www.contentstack.com/docs/guide/users-and-roles#roles'>Roles</a>.
* @namespace Role
*/
export function Role (http, data) {
this.urlPath = `/roles`
this.stackHeaders = data.stackHeaders
if (data.role) {
Object.assign(this, cloneDeep(data.role))
this.urlPath = `/roles/${this.uid}`
if (this.stackHeaders) {
/**
* @description The Update role call lets you modify an existing role of your stack.
* @memberof Role
* @func update
* @returns {Promise<Role.Role>} Promise for Role instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).role('role_uid').fetch({ include_rules: true, include_permissions: true})
* .then((role) => {
* role.name = 'My New Role'
* role.description = 'Role description'
* role.rules = [
* {
* module: 'asset',
* assets: ['$all'],
* acl: {
* read: true,
* create: true,
* update: true,
* publish: true,
* delete: true
* }
* },
* {
* module: 'environment',
* environments: [],
* acl: { read: true }
* },
* {
* module: 'locale',
* locales: [Array],
* acl: { read: true }
* }]
* return role.update()
* })
* .then((role) => console.log(role))
*
*/
this.update = update(http, 'role')
/**
* @description The Delete role call deletes an existing role from your stack.
* @memberof Role
* @func delete
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).role('role_uid').delete()
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http)
/**
* @description The Get a single role request returns comprehensive information on a specific role.
* @memberof Role
* @func fetch
* @returns {Promise<Role.Role>} Promise for Role instance
* @param {Boolean} include_permissions Set this parameter to 'true' to include the details of the permissions assigned to a particular role.
* @param {Boolean} include_rules Set this to ‘true’ to include the details of the rules assigned to a role.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).role('role_uid').fetch()
* .then((role) => console.log(role))
*
*/
this.fetch = fetch(http, 'role')
}
} else {
/**
* @description The Create a role call creates a new role in a stack.
* @memberof Role
* @func create
* @returns {Promise<Role.Role>} Promise for Role instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const role = {
* name: 'Role Name',
* description: 'From CMA Js',
* rules:
* [
* {
* module: 'environment',
* environments: [],
* acl: { read: true }
* },
* {
* module: 'locale',
* locales: [],
* acl: { read: true }
* }
* ],
* uid: 'uid'
* }
* client.stack().role().create({ role })
* .then((role) => console.log(role))
*/
this.create = create({ http: http })
/**
* @description The ‘Get all roles’ request returns comprehensive information about all roles created in a stack.
* @memberof Role
* @func findAll
* @param {Boolean} include_permissions Set this parameter to 'true' to include the details of the permissions assigned to a particular role.
* @param {Boolean} include_rules Set this to ‘true’ to include the details of the rules assigned to a role.
* @returns {ContentstackCollection} Instance of ContentstackCollection.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().role().findAll()
* .then((collection) => console.log(collection))
*/
this.fetchAll = fetchAll(http, RoleCollection)
/**
* @description The Query on Role will allow to fetch details of all or specific role.
* @memberof Role
* @func query
* @param {Boolean} include_permissions Set this parameter to 'true' to include the details of the permissions assigned to a particular role.
* @param {Boolean} include_rules Set this to ‘true’ to include the details of the rules assigned to a role.
* @returns {ContentstackCollection} Instance of ContentstackCollection.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).role().query({ query: { filename: 'Asset Name' } }).find()
* .then((role) => console.log(role))
*/
this.query = query({ http: http, wrapperCollection: RoleCollection })
}
return this
}
export function RoleCollection (http, data) {
const obj = cloneDeep(data.roles || [])
const roleCollection = obj.map((userdata) => {
return new Role(http, { role: userdata, stackHeaders: data.stackHeaders })
})
return roleCollection
}