@budibase/worker 2.8.29-alpha.0 → 2.8.29-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,176 +0,0 @@
1
- import env from "../../environment"
2
- import { events, accounts, tenancy } from "@budibase/backend-core"
3
- import { User, UserRoles, CloudAccount } from "@budibase/types"
4
-
5
- export const handleDeleteEvents = async (user: any) => {
6
- await events.user.deleted(user)
7
-
8
- if (isBuilder(user)) {
9
- await events.user.permissionBuilderRemoved(user)
10
- }
11
-
12
- if (isAdmin(user)) {
13
- await events.user.permissionAdminRemoved(user)
14
- }
15
- }
16
-
17
- const assignAppRoleEvents = async (
18
- user: User,
19
- roles: UserRoles,
20
- existingRoles: UserRoles
21
- ) => {
22
- for (const [appId, role] of Object.entries(roles)) {
23
- // app role in existing is not same as new
24
- if (!existingRoles || existingRoles[appId] !== role) {
25
- await events.role.assigned(user, role)
26
- }
27
- }
28
- }
29
-
30
- const unassignAppRoleEvents = async (
31
- user: User,
32
- roles: UserRoles,
33
- existingRoles: UserRoles
34
- ) => {
35
- if (!existingRoles) {
36
- return
37
- }
38
- for (const [appId, role] of Object.entries(existingRoles)) {
39
- // app role in new is not same as existing
40
- if (!roles || roles[appId] !== role) {
41
- await events.role.unassigned(user, role)
42
- }
43
- }
44
- }
45
-
46
- const handleAppRoleEvents = async (user: any, existingUser: any) => {
47
- const roles = user.roles
48
- const existingRoles = existingUser?.roles
49
-
50
- await assignAppRoleEvents(user, roles, existingRoles)
51
- await unassignAppRoleEvents(user, roles, existingRoles)
52
- }
53
-
54
- export const handleSaveEvents = async (
55
- user: User,
56
- existingUser: User | undefined
57
- ) => {
58
- const tenantId = tenancy.getTenantId()
59
- let tenantAccount: CloudAccount | undefined
60
- if (!env.SELF_HOSTED && !env.DISABLE_ACCOUNT_PORTAL) {
61
- tenantAccount = await accounts.getAccountByTenantId(tenantId)
62
- }
63
- await events.identification.identifyUser(user, tenantAccount)
64
-
65
- if (existingUser) {
66
- await events.user.updated(user)
67
-
68
- if (isRemovingBuilder(user, existingUser)) {
69
- await events.user.permissionBuilderRemoved(user)
70
- }
71
-
72
- if (isRemovingAdmin(user, existingUser)) {
73
- await events.user.permissionAdminRemoved(user)
74
- }
75
-
76
- if (isOnboardingComplete(user, existingUser)) {
77
- await events.user.onboardingComplete(user)
78
- }
79
-
80
- if (
81
- !existingUser.forceResetPassword &&
82
- user.forceResetPassword &&
83
- user.password
84
- ) {
85
- await events.user.passwordForceReset(user)
86
- }
87
-
88
- if (user.password !== existingUser.password) {
89
- await events.user.passwordUpdated(user)
90
- }
91
- } else {
92
- await events.user.created(user)
93
- }
94
-
95
- if (isAddingBuilder(user, existingUser)) {
96
- await events.user.permissionBuilderAssigned(user)
97
- }
98
-
99
- if (isAddingAdmin(user, existingUser)) {
100
- await events.user.permissionAdminAssigned(user)
101
- }
102
-
103
- await handleAppRoleEvents(user, existingUser)
104
- }
105
-
106
- const isBuilder = (user: any) => user.builder && user.builder.global
107
- const isAdmin = (user: any) => user.admin && user.admin.global
108
-
109
- export const isAddingBuilder = (user: any, existingUser: any) => {
110
- return isAddingPermission(user, existingUser, isBuilder)
111
- }
112
-
113
- export const isRemovingBuilder = (user: any, existingUser: any) => {
114
- return isRemovingPermission(user, existingUser, isBuilder)
115
- }
116
-
117
- const isAddingAdmin = (user: any, existingUser: any) => {
118
- return isAddingPermission(user, existingUser, isAdmin)
119
- }
120
-
121
- const isRemovingAdmin = (user: any, existingUser: any) => {
122
- return isRemovingPermission(user, existingUser, isAdmin)
123
- }
124
-
125
- const isOnboardingComplete = (user: any, existingUser: any) => {
126
- return !existingUser?.onboardedAt && typeof user.onboardedAt === "string"
127
- }
128
-
129
- /**
130
- * Check if a permission is being added to a new or existing user.
131
- */
132
- const isAddingPermission = (
133
- user: any,
134
- existingUser: any,
135
- hasPermission: any
136
- ) => {
137
- // new user doesn't have the permission
138
- if (!hasPermission(user)) {
139
- return false
140
- }
141
-
142
- // existing user has the permission
143
- if (existingUser && hasPermission(existingUser)) {
144
- return false
145
- }
146
-
147
- // permission is being added
148
- return true
149
- }
150
-
151
- /**
152
- * Check if a permission is being removed from an existing user.
153
- */
154
- const isRemovingPermission = (
155
- user: any,
156
- existingUser: any,
157
- hasPermission: any
158
- ) => {
159
- // new user has the permission
160
- if (hasPermission(user)) {
161
- return false
162
- }
163
-
164
- // no existing user or existing user doesn't have the permission
165
- if (!existingUser) {
166
- return false
167
- }
168
-
169
- // existing user doesn't have the permission
170
- if (!hasPermission(existingUser)) {
171
- return false
172
- }
173
-
174
- // permission is being removed
175
- return true
176
- }