-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathAddProjectMemberDialog.vue
More file actions
212 lines (185 loc) · 5.07 KB
/
AddProjectMemberDialog.vue
File metadata and controls
212 lines (185 loc) · 5.07 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<script>
import { Card } from '@components/Card';
import ProjectMemberEditor from '@shell/components/form/ProjectMemberEditor';
import AsyncButton from '@shell/components/AsyncButton';
import Banner from '@components/Banner/Banner.vue';
import { NORMAN } from '@shell/config/types';
export default {
emits: ['close'],
components: {
Card,
ProjectMemberEditor,
AsyncButton,
Banner
},
props: {
resources: {
type: Array,
required: true
},
onAdd: {
type: Function,
default: () => {}
},
projectId: {
type: String,
default: null
},
saveInModal: {
type: Boolean,
default: false
}
},
data() {
return {
member: {
permissionGroup: 'member',
custom: {},
principalId: '',
roleTemplateIds: []
},
error: null,
duplicateWarning: null
};
},
computed: {
principal() {
const principalId = this.member.principalId.replace(/\//g, '%2F');
return this.$store.dispatch('rancher/find', {
type: NORMAN.PRINCIPAL,
id: this.member.principalId,
opt: { url: `/v3/principals/${ principalId }` }
}, { root: true });
},
},
methods: {
async principalProperty() {
const principal = await this.principal;
return principal?.principalType === 'group' ? 'groupPrincipalId' : 'userPrincipalId';
},
close() {
this.$emit('close');
},
async apply() {
this.onAdd(await this.createBindings());
this.close();
},
// Returns the subset of roleTemplateIds that already exist for this principal+project.
existingRoleTemplateIds(principalProperty, principalId) {
const all = this.$store.getters['rancher/all'](NORMAN.PROJECT_ROLE_TEMPLATE_BINDING);
return this.member.roleTemplateIds.filter((roleTemplateId) => all.some(
(b) => b.projectId === this.projectId &&
b.roleTemplateId === roleTemplateId &&
b[principalProperty] === principalId
));
},
async createBindings() {
const principalProperty = await this.principalProperty();
const principalId = this.member.principalId;
const duplicates = this.existingRoleTemplateIds(principalProperty, principalId);
const newRoleTemplateIds = this.member.roleTemplateIds.filter((id) => !duplicates.includes(id));
if (newRoleTemplateIds.length === 0) {
throw new Error(this.t('addProjectMemberDialog.duplicateRolesError'));
}
if (duplicates.length > 0) {
this.duplicateWarning = this.t('addProjectMemberDialog.duplicateRolesSkipped');
}
const promises = newRoleTemplateIds.map((roleTemplateId) => this.$store.dispatch(`rancher/create`, {
type: NORMAN.PROJECT_ROLE_TEMPLATE_BINDING,
roleTemplateId,
[principalProperty]: principalId,
projectId: this.projectId,
}));
return Promise.all(promises);
},
saveBindings(btnCB) {
this.error = null;
this.duplicateWarning = null;
this.createBindings()
.then((bindings) => {
return Promise.all(bindings.map((b) => b.save()));
})
.then(() => {
btnCB(true);
setTimeout(this.close, 500);
})
.catch((err) => {
this.error = err;
btnCB(false);
});
}
}
};
</script>
<template>
<Card
class="prompt-rotate"
:show-highlight-border="false"
:sticky="true"
>
<template #title>
<h4
v-clean-html="t('addProjectMemberDialog.title')"
class="text-default-text"
/>
</template>
<template #body>
<div class="pl-10 pr-10">
<Banner
v-if="error"
color="error"
>
{{ error }}
</Banner>
<Banner
v-if="duplicateWarning"
color="warning"
>
{{ duplicateWarning }}
</Banner>
<ProjectMemberEditor
v-model:value="member"
:use-two-columns-for-custom="true"
/>
</div>
</template>
<template #actions>
<div class="buttons">
<button
class="btn role-secondary mr-10"
@click="close"
>
{{ t('generic.cancel') }}
</button>
<AsyncButton
v-if="saveInModal"
mode="create"
@click="cb=>saveBindings(cb)"
/>
<button
v-else
class="btn role-primary"
@click="apply"
>
{{ t('generic.add') }}
</button>
</div>
</template>
</Card>
</template>
<style lang='scss' scoped>
.prompt-rotate {
margin: 0;
}
.buttons {
display: flex;
justify-content: flex-end;
width: 100%;
}
</style>
<style lang="scss">
.card-container {
border: 1px solid var(--border);
box-shadow: none;
}
</style>