Tokens.vue 31.5 KB
Newer Older
janis.streib's avatar
janis.streib committed
1
<template>
gj4210's avatar
gj4210 committed
2
  <div id="Tokens">
janis.streib's avatar
janis.streib committed
3
    <h1 class="my-4">Unterkonten & API-Tokens</h1>
janis.streib's avatar
janis.streib committed
4
    <b-modal id="modal-create-account" size="lg" title="Unterkonto erstellen"
gj4210's avatar
gj4210 committed
5
6
7
8
             @hidden="resetAccountData">
      <b-form @submit="createAccount">
        <b-form-group label="Beschreibung:" label-for="input-account-create-description">
          <b-form-textarea
gj4210's avatar
gj4210 committed
9
10
11
            id="input-account-create-description"
            v-model.trim="new_account.description"
            placeholder="Beschreibung eingeben"
gj4210's avatar
gj4210 committed
12
13
          />
        </b-form-group>
gj4210's avatar
gj4210 committed
14
15
        <template
          v-if="roles_by_account != null && roles_by_account[$store.state.user.login_name] != null && assignable_role_name_options_by_system != null">
gj4210's avatar
gj4210 committed
16
17
18
          <b-form-group label="Rollen">
            <b-form-radio v-model="new_account.do_copy_roles" :value="true">von Parent-Account übernehmen
            </b-form-radio>
gj4210's avatar
gj4210 committed
19
            <b-form-radio v-model="new_account.do_copy_roles" :value="false" class="mb-2">individuell zuweisen
gj4210's avatar
gj4210 committed
20
            </b-form-radio>
gj4210's avatar
gj4210 committed
21
22
23
24
25
26
27
            <template v-if="!new_account.do_copy_roles">
              <b-input-group>
                <b-form-select @change="updateCanAddRole" v-model="new_system"
                               :options="assignable_system_options"/>
                <b-form-select @change="updateCanAddRole" v-model="new_role_name"
                               :options="assignable_role_name_options_by_system[new_system]"/>
                <b-input-group-append>
gj4210's avatar
gj4210 committed
28
29
30
                  <b-button variant="success" :disabled="!can_add_role" @click="addRole">
                    <template v-if="cant_add_role_reason === ''">Rolle hinzufügen</template>
                    <template v-else class="text-danger">{{ cant_add_role_reason }}</template>
gj4210's avatar
gj4210 committed
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
                  </b-button>
                </b-input-group-append>
              </b-input-group>
              <h5 class="my-1">
                <template
                  v-for="role in new_account.roles">
                  <b-button
                    :key="'role-badge-' + role.mgr_login_name + '-' + role.fq_name"
                    :id="'create-account-role-badge-' + role.fq_name"
                    :style="{background: role.fq_name.toHSL({lit: [30, 40]}), border: 'none'}"
                    class="mr-1 mb-1 badge">
                    <div class="d-flex align-items-center">
                      {{ role.system.toUpperCase() }}<br>{{ role.name }}
                      <span @click="removeRole(role)" class="p-2">
                      <font-awesome-icon style="opacity: 0.7" :icon="['fas','times']"/>
                        </span>
                    </div>
                  </b-button>
                  <b-popover
                    :key="'role-badge-tooltip' + role.mgr_login_name + '-' + role.fq_name"
                    :target="'create-account-role-badge-' + role.fq_name"
                    triggers="focus" custom-class="popover-wide"
                    placement="bottom">
                    <b-table :fields="permission_list_fields"
                             :items="role.contained_permissions"
                             sticky-header/>
                  </b-popover>
                </template>
              </h5>
gj4210's avatar
gj4210 committed
60
            </template>
gj4210's avatar
gj4210 committed
61
          </b-form-group>
gj4210's avatar
gj4210 committed
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
        </template>
      </b-form>
      <template v-slot:modal-footer="{cancel}">
        <b-button variant="outline-secondary" @click="cancel">
          Abbrechen
        </b-button>
        <b-button type="submit" variant="success" :disabled="waiting" @click="createAccount">
          Erstellen
        </b-button>
      </template>
    </b-modal>
    <b-row>
      <b-col lg="9">
        <b-input-group class="shadow">
          <b-input-group-prepend>
            <b-input-group-text>
              <font-awesome-icon :icon="['fas', 'filter']"/>
            </b-input-group-text>
          </b-input-group-prepend>
          <b-form-input id="filter-input" v-model.trim="filter_text" placeholder="Filter"
                        autofocus debounce="300"/>
        </b-input-group>
      </b-col>
      <b-col lg="3" sm="*">
        <b-button block variant="outline-success" v-b-modal.modal-create-account class="shadow">
janis.streib's avatar
janis.streib committed
87
          Unterkonto erstellen
gj4210's avatar
gj4210 committed
88
89
90
91
92
93
94
95
        </b-button>
      </b-col>
    </b-row>
    <b-modal id="modal-edit-account" size="lg" title="Account bearbeiten"
             @hidden="resetAccountData">
      <b-form>
        <b-form-group label="Beschreibung:" label-for="input-account-edit-description">
          <b-form-textarea
gj4210's avatar
gj4210 committed
96
97
98
            id="input-account-edit-description"
            v-model.trim="new_account.description"
            placeholder="Beschreibung eingeben"
gj4210's avatar
gj4210 committed
99
100
          />
        </b-form-group>
gj4210's avatar
gj4210 committed
101
102
        <template
          v-if="roles_by_account != null && roles_by_account[$store.state.user.login_name] != null && assignable_role_name_options_by_system != null">
gj4210's avatar
gj4210 committed
103
          <b-form-group label="Rollen">
gj4210's avatar
gj4210 committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
            <b-input-group>
              <b-form-select @change="updateCanAddRole" v-model="new_system"
                             :options="assignable_system_options"/>
              <b-form-select @change="updateCanAddRole" v-model="new_role_name"
                             :options="assignable_role_name_options_by_system[new_system]"/>
              <b-input-group-append>
                <b-button variant="success" :disabled="!can_add_role" @click="addRole">
                  <template v-if="cant_add_role_reason === ''">Rolle hinzufügen</template>
                  <template v-else class="text-danger">{{ cant_add_role_reason }}</template>
                </b-button>
              </b-input-group-append>
            </b-input-group>
            <h5 class="my-1">
              <template
                v-for="role in new_account.roles">
                <b-button
                  :key="'role-badge-' + role.mgr_login_name + '-' + role.fq_name"
                  :id="'create-account-role-badge-' + role.fq_name"
                  :style="{background: role.fq_name.toHSL({lit: [30, 40]}), border: 'none'}"
                  class="mr-1 mb-1 badge">
                  <div class="d-flex align-items-center">
                    {{ role.system.toUpperCase() }}<br>{{ role.name }}
                    <span @click="removeRole(role)" class="p-2">
gj4210's avatar
gj4210 committed
127
128
                      <font-awesome-icon style="opacity: 0.7" :icon="['fas','times']"/>
                        </span>
gj4210's avatar
gj4210 committed
129
130
131
132
133
134
135
136
137
138
139
140
141
                  </div>
                </b-button>
                <b-popover
                  :key="'role-badge-tooltip' + role.mgr_login_name + '-' + role.fq_name"
                  :target="'create-account-role-badge-' + role.fq_name"
                  triggers="focus" custom-class="popover-wide"
                  placement="bottom">
                  <b-table :fields="permission_list_fields"
                           :items="role.contained_permissions"
                           sticky-header/>
                </b-popover>
              </template>
            </h5>
gj4210's avatar
gj4210 committed
142
143
          </b-form-group>
        </template>
gj4210's avatar
gj4210 committed
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
      </b-form>
      <template v-slot:modal-footer="{cancel}">
        <b-alert id="alert-edit-account" v-model="show_modal_alert"
                 variant="danger" dismissible fade class="mb-0 flex-grow">
          {{ modal_alert_content }}
        </b-alert>
        <b-button variant="outline-secondary" @click="cancel">
          Abbrechen
        </b-button>
        <b-button id="button-delete-account" variant="danger">
          Löschen
        </b-button>
        <b-popover ref="popoverAccountDelete" target="button-delete-account" triggers="click"
                   placement="bottom">
          <template v-slot:title>Account wirklich löschen?</template>
          <b-button variant="danger" :disabled="waiting" @click="deleteAccount">
            Löschen
          </b-button>
          <b-button variant="outline-secondary" @click="$refs.popoverAccountDelete.$emit('close')">
            Abbrechen
          </b-button>
        </b-popover>
        <b-button variant="primary" :disabled="waiting" @click="editAccount">
          Änderungen übernehmen
        </b-button>
      </template>
    </b-modal>
    <b-modal id="modal-create-token" size=lg title="Token erstellen"
             @hidden="resetTokenData">
      <b-form>
        <b-form-group label="Beschreibung:" label-for="input-token-create-description">
          <b-form-textarea
gj4210's avatar
gj4210 committed
176
177
178
179
            id="input-token-create-description"
            v-model.trim="new_token.description"
            required
            placeholder="Beschreibung"
gj4210's avatar
gj4210 committed
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
          />
        </b-form-group>
        <div v-if="new_token.expiration_date != null">
          <DateTimePicker label="Ablaufdatum:" :model="new_token.expiration_date"/>
          <b-button block variant="outline-secondary" @click="new_token.expiration_date = null">
            Ablaufdatum entfernen
          </b-button>
        </div>
        <b-button block variant="outline-secondary" v-else
                  @click="new_token.expiration_date = formatDate(getDate30DaysAhead())">
          Ablaufdatum hinzufügen
        </b-button>
      </b-form>
      <template v-slot:modal-footer="{cancel}">
        <b-alert id="alert-create-token" v-model="show_modal_alert"
                 variant="danger" dismissible fade class="mb-0 flex-grow">
          {{ modal_alert_content }}
        </b-alert>
        <b-button variant="outline-secondary" @click="cancel">
          Abbrechen
        </b-button>
        <b-button type="submit" variant="success" :disabled="waiting" @click="createToken">
          Erstellen
        </b-button>
      </template>
    </b-modal>
    <b-modal id="modal-token" size=lg title="Token erstellt">
gj4210's avatar
gj4210 committed
207
      <CopyField class="shadow" :text="token"/>
gj4210's avatar
gj4210 committed
208
209
210
211
212
213
214
215
216
217
218
219
220
      <b-alert show variant="warning" class="mb-0 mt-3">Bewahren Sie das Token gut auf. Hier werden Sie es nicht
        mehr einsehen können!
      </b-alert>
      <template v-slot:modal-footer="{ok}">
        <b-button variant="success" @click="ok">
          Ok
        </b-button>
      </template>
    </b-modal>
    <b-modal id="modal-edit-token" size=lg title="Token bearbeiten">
      <b-form>
        <b-form-group label="Beschreibung:" label-for="input-token-edit-description">
          <b-form-textarea
gj4210's avatar
gj4210 committed
221
222
223
224
            id="input-token-edit-description"
            v-model.trim="new_token.description"
            required
            placeholder="Beschreibung"
gj4210's avatar
gj4210 committed
225
226
227
228
229
230
231
232
233
234
235
236
237
          />
        </b-form-group>
      </b-form>
      <div v-if="new_token.expiration_date != null">
        <DateTimePicker label="Ablaufdatum:" :model="new_token.expiration_date"/>
        <b-button block variant="outline-secondary" @click="new_token.expiration_date = null">
          Ablaufdatum entfernen
        </b-button>
      </div>
      <b-button block variant="outline-secondary" v-else
                @click="new_token.expiration_date = formatDate(getDate30DaysAhead())">
        Ablaufdatum hinzufügen
      </b-button>
janis.streib's avatar
janis.streib committed
238
      <template v-slot:modal-footer="{cancel}">
gj4210's avatar
gj4210 committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
        <b-alert id="alert-edit-token" v-model="show_modal_alert"
                 variant="danger" dismissible fade class="mb-0 flex-grow">
          {{ modal_alert_content }}
        </b-alert>
        <b-button variant="outline-secondary" @click="cancel">
          Abbrechen
        </b-button>
        <b-button id="button-delete-token" variant="danger">
          Löschen
        </b-button>
        <b-popover ref="popoverTokenDelete" target="button-delete-token" triggers="click" placement="bottom">
          <template v-slot:title>Token wirklich löschen?</template>
          <b-button variant="danger" @click="deleteToken">
            Löschen
          </b-button>
          <b-button variant="outline-secondary" @click="$refs.popoverTokenDelete.$emit('close')">
            Abbrechen
          </b-button>
        </b-popover>
        <b-button type="submit" variant="primary" :disabled="waiting" @click="editToken">
          Änderungen übernehmen
        </b-button>
      </template>
    </b-modal>
    <Loading :data="[filtered_accounts]">
      <template v-for="account in filtered_accounts">
        <b-card no-body :key="'card-account-' + account.login_name" class="mb-4 shadow">
          <template v-slot:header>
            <b-row>
              <b-col lg="3">
                <h4>
                  {{ account.login_name }}
                  <b-badge variant="success">
                    <template v-if="account.login_name in tokens_by_account">
                      <template v-if="tokens_by_account[account.login_name].length === 1">
                        1 Token
                      </template>
                      <template v-else>{{ tokens_by_account[account.login_name].length }} Tokens
                      </template>
278
                    </template>
gj4210's avatar
gj4210 committed
279
280
281
282
283
284
285
286
287
                    <template v-else>0 Tokens</template>
                  </b-badge>
                </h4>
                <p class="text-muted">Login Name</p>
              </b-col>
              <b-col>
                <div v-if="account.parent_login_name === account.login_name">Hauptaccount; enthält nur Session-Tokens;
                  kann
                  nicht gelöscht werden.
288
                </div>
gj4210's avatar
gj4210 committed
289
290
291
292
293
294
295
                <div v-else-if="account.description === '' || account.description == null">Keine
                  Beschreibung vorhanden.
                </div>
                <div v-else>{{ account.description }}</div>
                <p class="text-muted">Beschreibung</p>
              </b-col>
              <b-col>
gj4210's avatar
gj4210 committed
296
                <template v-if="roles_by_account[account.login_name] != null && assignable_roles_by_fq_name != null">
gj4210's avatar
gj4210 committed
297
298
                  <h5 class="mb-0">
                    <template
gj4210's avatar
gj4210 committed
299
                      v-for="role in roles_by_account[account.login_name].slice(0, max_role_badge_count)">
gj4210's avatar
gj4210 committed
300
                      <b-button
gj4210's avatar
gj4210 committed
301
302
303
304
                        :key="'role-badge-' + role.mgr_login_name + '-' + role.role_fq_name"
                        :id="account.login_name + '-role-badge-' + role.mgr_login_name + '-' + role.role_fq_name"
                        :style="{background: role.role_fq_name.toHSL({lit: [30, 40]}), border: 'none'}"
                        class="mr-1 mb-1 badge">{{ role.system.toUpperCase() }}<br>{{ role.role }}
gj4210's avatar
gj4210 committed
305
306
                      </b-button>
                      <b-popover
gj4210's avatar
gj4210 committed
307
308
309
310
                        :key="'role-badge-tooltip' + role.mgr_login_name + '-' + role.role_fq_name"
                        :target="account.login_name + '-role-badge-' + role.mgr_login_name + '-' + role.role_fq_name"
                        triggers="focus" custom-class="popover-wide"
                        placement="bottom">
gj4210's avatar
gj4210 committed
311
                        <b-table :fields="permission_list_fields"
gj4210's avatar
gj4210 committed
312
                                 :items="assignable_roles_by_fq_name[role.role_fq_name][0].contained_permissions"
gj4210's avatar
gj4210 committed
313
314
                                 sticky-header/>
                      </b-popover>
315
                    </template>
gj4210's avatar
gj4210 committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
                  </h5>
                  <h6 v-if="roles_by_account[account.login_name].length > max_role_badge_count">
                    + {{ roles_by_account[account.login_name].length - max_role_badge_count }} Weitere
                  </h6>
                </template>
                <h6 v-else>Keine Rollen zugewiesen</h6>
                <p class="text-muted">Rollen</p>
              </b-col>
              <b-col lg="2">
                <template v-if="account.parent_login_name !== account.login_name">
                  <b-button block variant="outline-primary"
                            :id="'button-edit-account-' + account.login_name"
                            @click="showModalEditAccount(account)">
                    <font-awesome-icon :icon="['far', 'edit']"/>
                  </b-button>
                  <b-tooltip placement="bottom" :target="'button-edit-account-' + account.login_name"
                             triggers="hover" variant="primary">
                    Account bearbeiten
                  </b-tooltip>
                </template>
              </b-col>
            </b-row>
          </template>
          <b-button block squared variant="outline-secondary" v-b-toggle="account.login_name + '-collapse'">
gj4210's avatar
gj4210 committed
340
            <font-awesome-icon class="collapse-icon" :icon="['fas','chevron-down']"/>
gj4210's avatar
gj4210 committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
          </b-button>
          <b-collapse :id="account.login_name + '-collapse'">
            <b-table :items="tokens_by_account[account.login_name]"
                     :fields="token_list_fields"
                     class="m-0"
                     striped
                     responsive
                     primary-key="description">
              <template v-slot:head(buttons)>
                <template v-if="account.parent_login_name !== account.login_name">
                  <b-button block variant="outline-success"
                            :id="'button-create-token-' +  account.login_name"
                            @click="showModalCreateToken(account.login_name)">
                    <font-awesome-icon :icon="['fas', 'plus']"/>
                  </b-button>
                  <b-tooltip :target="'button-create-token-' +  account.login_name" triggers="hover"
                             variant="success" placement="left">
                    Token erstellen
                  </b-tooltip>
                </template>
              </template>
              <template v-slot:cell(expiration_date)="data">
                {{ formatDate(data.item.expiration_date) }}
                <template v-if="data.item.is_expired">
                  (Expired)
                </template>
              </template>
              <template v-slot:cell(buttons)="data">
                <b-button block variant="outline-primary" v-if="account.parent_login_name !== account.login_name"
                          :id="'button-edit-token-' + account.login_name+ '-' + data.index"
                          @click="showModalEditToken(data.item)">
                  <font-awesome-icon :icon="['far', 'edit']"/>
                </b-button>
                <b-tooltip :target="'button-edit-token-' + account.login_name+ '-' + data.index"
                           triggers="hover" variant="primary" placement="left"
                           v-if="account.parent_login_name !== account.login_name">
                  Token bearbeiten
                </b-tooltip>
              </template>
            </b-table>
          </b-collapse>
        </b-card>
      </template>
    </Loading>
  </div>
janis.streib's avatar
janis.streib committed
386
387
388
</template>

<script>
janis.streib's avatar
janis.streib committed
389
import AccountTokenService from '@/api-services/account_token.service'
gj4210's avatar
gj4210 committed
390
391
392
393
import AccountService from '@/api-services.gen/cntl.mgr'
import TokenService from '@/api-services.gen/cntl.wapi_auth'
import ApiUtil from '@/util/apiutil'
import '@/util/colorutil'
janis.streib's avatar
janis.streib committed
394
import DateTimePicker from '../../components/DateTimePicker'
gj4210's avatar
gj4210 committed
395
396
import Mgr2RoleService from '@/api-services.gen/cntl.mgr2role'
import RoleService from '@/api-services.gen/cntl.role'
janis.streib's avatar
janis.streib committed
397
398
import Loading from '../../components/Loading'
import CopyField from '../../components/CopyField'
janis.streib's avatar
janis.streib committed
399

gj4210's avatar
gj4210 committed
400
401
export default {
  name: 'tokens',
gj4210's avatar
gj4210 committed
402
403
  components: {CopyField, Loading, DateTimePicker},
  data() {
gj4210's avatar
gj4210 committed
404
405
406
    return {
      tokens_by_account: null,
      roles_by_account: null,
gj4210's avatar
gj4210 committed
407
408
409
410
411
      assignable_roles: null,
      assignable_roles_by_fq_name: null,
      assignable_roles_by_system: null,
      assignable_system_options: null,
      assignable_role_name_options_by_system: null,
gj4210's avatar
gj4210 committed
412
413
414
415
416
417
418
      accounts: null,
      new_account: {
        parent_login_name: null,
        description: '',
        login_name: '',
        expiration_date: null,
        do_copy_roles: true,
gj4210's avatar
gj4210 committed
419
        roles: []
gj4210's avatar
gj4210 committed
420
421
422
423
424
425
      },
      new_token: {
        description: '',
        login_name: '',
        expiration_date: null
      },
janis.streib's avatar
janis.streib committed
426
      filter_text: '',
gj4210's avatar
gj4210 committed
427
428
429
      token_list_fields: [
        {
          key: 'description',
janis.streib's avatar
janis.streib committed
430
          label: 'Beschreibung',
gj4210's avatar
gj4210 committed
431
          sortable: false
janis.streib's avatar
janis.streib committed
432
        },
gj4210's avatar
gj4210 committed
433
434
        {
          key: 'expiration_date',
janis.streib's avatar
janis.streib committed
435
          label: 'Ablaufdatum',
gj4210's avatar
gj4210 committed
436
          sortable: true
437
        },
gj4210's avatar
gj4210 committed
438
439
        {
          key: 'last_login_date',
janis.streib's avatar
janis.streib committed
440
          label: 'Zuletzt verwendet',
gj4210's avatar
gj4210 committed
441
442
          formatter: this.formatDate,
          sortable: true
443
        },
gj4210's avatar
gj4210 committed
444
445
        {
          key: 'last_generate_date',
janis.streib's avatar
janis.streib committed
446
          label: 'Zuletzt generiert',
gj4210's avatar
gj4210 committed
447
448
449
450
451
452
453
          formatter: this.formatDate,
          sortable: true
        },
        {
          key: 'buttons',
          label: '',
          sortable: false
janis.streib's avatar
janis.streib committed
454
        }
gj4210's avatar
gj4210 committed
455
456
457
458
      ],
      permission_list_fields: [
        {
          key: 'system',
janis.streib's avatar
janis.streib committed
459
          label: 'System'
gj4210's avatar
gj4210 committed
460
461
462
463
        },
        {
          key: 'permission',
          label: 'Berechtigung'
janis.streib's avatar
janis.streib committed
464
        }
gj4210's avatar
gj4210 committed
465
466
      ],
      show_modal_alert: true,
janis.streib's avatar
janis.streib committed
467
      modal_alert_content: '',
gj4210's avatar
gj4210 committed
468
      waiting: false,
janis.streib's avatar
janis.streib committed
469
      token: '',
gj4210's avatar
gj4210 committed
470
      max_role_badge_count: 10,
gj4210's avatar
gj4210 committed
471
472
473
      max_permission_count: 10,
      new_system: '',
      new_role_name: '',
474
475
      can_add_role: false,
      cant_add_role_reason: ''
gj4210's avatar
gj4210 committed
476
477
478
    }
  },
  computed: {
gj4210's avatar
gj4210 committed
479
    filtered_accounts() {
gj4210's avatar
gj4210 committed
480
481
482
483
484
485
486
      if (this.accounts == null) {
        return null
      }
      if (this.filter_text === '') {
        return this.accounts
      }
      return this.accounts.filter(account => {
janis.streib's avatar
janis.streib committed
487
        return account.login_name.toLowerCase().includes(this.filter_text.toLowerCase()) ||
gj4210's avatar
gj4210 committed
488
          (account.description != null && account.description.toLowerCase().includes(this.filter_text.toLowerCase()))
gj4210's avatar
gj4210 committed
489
490
491
      })
    }
  },
gj4210's avatar
gj4210 committed
492
  created() {
gj4210's avatar
gj4210 committed
493
494
495
    this.fetchData()
  },
  methods: {
gj4210's avatar
gj4210 committed
496
497
    fetchData() {
      AccountTokenService.list(this.$store.state.netdb_axios_config, this.$store.state.user.login_name).then(response => {
gj4210's avatar
gj4210 committed
498
499
500
501
502
        this.accounts = response.data[0]
        this.accounts.sort((a, b) => {
          return a.parent_login_name === a.login_name ? -1 : b.parent_login_name === b.login_name ? 1 : 0
        })
        this.tokens_by_account = ApiUtil.dict_of_lists_by_value_of_array(this.formatExpiredTokens(response.data[1]), 'login_name')
gj4210's avatar
gj4210 committed
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
        this.roles_by_account = ApiUtil.dict_of_lists_by_value_of_array(response.data[2], 'mgr_login_name')
        const assignable_role_names = new Set()
        response.data[3].forEach(role => {
          assignable_role_names.add(role.fq_name)
          role.contained_roles.forEach(contained_role => {
            assignable_role_names.add(contained_role)
          })
        })
        RoleService.list(this.$store.state.netdb_axios_config, {
          is_role: true,
          fq_name_list: Array.from(assignable_role_names)
        }).then(response => {
          this.assignable_roles = response.data[0]
          this.assignable_roles_by_fq_name = ApiUtil.dict_of_lists_by_value_of_array(this.formatContainedPermissions(this.assignable_roles), 'fq_name')
          this.assignable_roles_by_system = ApiUtil.dict_of_lists_by_value_of_array(this.assignable_roles, 'system')
          this.assignable_system_options = new Set()
          this.assignable_role_name_options_by_system = {}
          Object.keys(this.assignable_roles_by_system).forEach(system => {
            this.assignable_system_options.add({value: system, text: system.toUpperCase()})
            this.assignable_role_name_options_by_system[system] = []
            this.assignable_roles_by_system[system].forEach(role => {
              this.assignable_role_name_options_by_system[system].push({value: role.name, text: role.name})
            })
          })
          this.assignable_system_options = Array.from(this.assignable_system_options)
        })
gj4210's avatar
gj4210 committed
529
530
      })
    },
gj4210's avatar
gj4210 committed
531
    showModalEditAccount(account) {
532
533
      this.new_system = ''
      this.new_role_name = ''
gj4210's avatar
gj4210 committed
534
535
536
      this.new_account.description = account.description
      this.new_account.login_name = account.login_name
      this.new_account.parent_login_name = account.parent_login_name
gj4210's avatar
gj4210 committed
537
      this.new_account.roles = []
538
539
540
541
542
543
      if (this.roles_by_account[account.login_name]) {
        this.roles_by_account[account.login_name].forEach(role => {
          this.new_account.roles.push(this.assignable_roles_by_fq_name[role.role_fq_name][0])
        })
      }
      this.updateCanAddRole()
gj4210's avatar
gj4210 committed
544
545
546
      this.show_modal_alert = false
      this.$bvModal.show('modal-edit-account')
    },
gj4210's avatar
gj4210 committed
547
    showModalEditToken(token) {
gj4210's avatar
gj4210 committed
548
549
550
551
552
553
554
      this.new_token.description = token.description
      this.new_token.login_name = token.login_name
      this.new_token.expiration_date = token.expiration_date != null ? this.formatDate(token.expiration_date) : null
      this.new_token.pk = token.pk
      this.show_modal_alert = false
      this.$bvModal.show('modal-edit-token')
    },
gj4210's avatar
gj4210 committed
555
    showModalCreateToken(login_name) {
gj4210's avatar
gj4210 committed
556
557
558
559
560
561
      this.new_token.description = ''
      this.new_token.expiration_date = null
      this.new_token.login_name = login_name
      this.show_modal_alert = false
      this.$bvModal.show('modal-create-token')
    },
gj4210's avatar
gj4210 committed
562
    resetAccountData() {
gj4210's avatar
gj4210 committed
563
564
565
566
567
568
      this.new_account = {
        parent_login_name: null,
        description: '',
        login_name: '',
        expiration_date: null,
        do_copy_roles: true,
gj4210's avatar
gj4210 committed
569
        roles: []
gj4210's avatar
gj4210 committed
570
571
      }
    },
gj4210's avatar
gj4210 committed
572
    resetTokenData() {
gj4210's avatar
gj4210 committed
573
574
575
576
577
578
      this.new_token = {
        description: '',
        login_name: '',
        expiration_date: null
      }
    },
gj4210's avatar
gj4210 committed
579
    createAccount() {
gj4210's avatar
gj4210 committed
580
581
582
583
584
585
586
587
588
      this.waiting = true
      AccountService.create(this.$store.state.netdb_axios_config, {
        do_copy_assignments_new: true,
        description_new: this.new_account.description,
        allow_data_manipulation_new: true,
        do_copy_roles_new: this.new_account.do_copy_roles
      }).then((response) => {
        if (!this.new_account.do_copy_roles) {
          this.new_account.login_name = response.data[0][0].login_name
janis.streib's avatar
janis.streib committed
589
          const roles = []
gj4210's avatar
gj4210 committed
590
591
          this.new_account.roles.forEach(role => {
            roles.push(role.fq_name)
gj4210's avatar
gj4210 committed
592
          })
gj4210's avatar
gj4210 committed
593
          Mgr2RoleService.bulk_update(this.$store.state.netdb_axios_config, {
gj4210's avatar
gj4210 committed
594
595
596
597
            mgr_login_name_old: this.new_account.login_name,
            role_fq_name_list_old: [],
            role_fq_name_list_new: roles
          }).then(() => {
gj4210's avatar
gj4210 committed
598
599
            this.$bvModal.hide('modal-create-account')
            this.fetchData()
gj4210's avatar
gj4210 committed
600
601
602
603
604
605
606
607
608
            this.waiting = false
          })
        }
      }).catch(error => {
        this.modal_alert_content = error.response.data.error.type.text_descr
        this.show_modal_alert = true
        this.waiting = false
      })
    },
gj4210's avatar
gj4210 committed
609
    editAccount() {
gj4210's avatar
gj4210 committed
610
      this.waiting = true
gj4210's avatar
gj4210 committed
611
      const old_roles = []
gj4210's avatar
gj4210 committed
612
613
614
615
616
      if (this.roles_by_account[this.new_account.login_name]) {
        this.roles_by_account[this.new_account.login_name].forEach(role => {
          old_roles.push(role.role_fq_name)
        })
      }
gj4210's avatar
gj4210 committed
617
618
619
620
621
622
      const new_roles = []
      this.new_account.roles.forEach(role => {
        new_roles.push(role.fq_name)
      })
      const roles_to_add = new_roles.filter(x => !old_roles.includes(x))
      const roles_to_remove = old_roles.filter(x => !new_roles.includes(x))
gj4210's avatar
gj4210 committed
623
624
625
626
627
      AccountTokenService.update(this.$store.state.netdb_axios_config, {
        description_new: this.new_account.description,
        login_name_old: this.new_account.login_name,
        login_name_new: this.new_account.login_name,
        allow_data_manipulation_new: true,
gj4210's avatar
gj4210 committed
628
629
        roles_to_add: roles_to_add,
        roles_to_remove: roles_to_remove
gj4210's avatar
gj4210 committed
630
631
632
633
634
635
636
637
638
639
      }).then(() => {
        this.$bvModal.hide('modal-edit-account')
        this.fetchData()
        this.waiting = false
      }).catch(error => {
        this.modal_alert_content = error.response.data.error.type.text_descr
        this.show_modal_alert = true
        this.waiting = false
      })
    },
gj4210's avatar
gj4210 committed
640
    deleteAccount() {
gj4210's avatar
gj4210 committed
641
642
643
644
645
646
647
648
649
650
651
652
653
654
      this.waiting = true
      AccountService.delete(this.$store.state.netdb_axios_config, {
        do_delete_references: true,
        login_name_old: this.new_account.login_name
      }).then(() => {
        this.$bvModal.hide('modal-edit-account')
        this.fetchData()
        this.waiting = false
      }).catch(error => {
        this.modal_alert_content = error.response.data.error.type.text_descr
        this.show_modal_alert = true
        this.waiting = false
      })
    },
gj4210's avatar
gj4210 committed
655
    createToken() {
gj4210's avatar
gj4210 committed
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
      this.waiting = true
      TokenService.create(this.$store.state.netdb_axios_config, {
        description_new: this.new_token.description,
        login_name_new: this.new_token.login_name,
        expiration_date_new: this.new_token.expiration_date
      }).then(response => {
        this.$bvModal.hide('modal-create-token')
        this.token = response.data[0][0].token
        this.$bvModal.show('modal-token')
        this.fetchData()
        this.waiting = false
      }).catch(error => {
        this.modal_alert_content = error.response.data.error.type.text_descr
        this.show_modal_alert = true
        this.waiting = false
      })
    },
gj4210's avatar
gj4210 committed
673
    editToken() {
gj4210's avatar
gj4210 committed
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
      this.waiting = true
      TokenService.update(this.$store.state.netdb_axios_config, {
        description_new: this.new_token.description,
        pk_old: this.new_token.pk,
        do_refresh_token_new: true,
        expiration_date_new: this.new_token.expiration_date
      }).then(() => {
        this.$bvModal.hide('modal-edit-token')
        this.fetchData()
        this.waiting = false
      }).catch(error => {
        this.modal_alert_content = error.response.data.error.type.text_descr
        this.show_modal_alert = true
        this.waiting = false
      })
    },
gj4210's avatar
gj4210 committed
690
    deleteToken() {
gj4210's avatar
gj4210 committed
691
692
693
694
695
696
697
698
699
700
701
702
703
      this.waiting = true
      TokenService.delete(this.$store.state.netdb_axios_config, {
        pk_old: this.new_token.pk
      }).then(() => {
        this.$bvModal.hide('modal-edit-token')
        this.fetchData()
        this.waiting = false
      }).catch(error => {
        this.modal_alert_content = error.response.data.error.type.text_descr
        this.show_modal_alert = true
        this.waiting = false
      })
    },
gj4210's avatar
gj4210 committed
704
    formatDate(value) {
gj4210's avatar
gj4210 committed
705
706
707
708
709
      if (value == null) {
        return 'N/A'
      }
      return new Date(Date.parse(value)).toLocaleString('de-DE')
    },
gj4210's avatar
gj4210 committed
710
    getDate30DaysAhead() {
janis.streib's avatar
janis.streib committed
711
      const d = new Date()
gj4210's avatar
gj4210 committed
712
713
714
      d.setDate(d.getDate() + 29)
      return d
    },
gj4210's avatar
gj4210 committed
715
    formatExpiredTokens(tokens) {
gj4210's avatar
gj4210 committed
716
      tokens.forEach(token => {
janis.streib's avatar
janis.streib committed
717
718
719
720
        if (token.is_expired) {
          token._rowVariant = 'danger'
        }
      }
gj4210's avatar
gj4210 committed
721
722
723
      )
      return tokens
    },
gj4210's avatar
gj4210 committed
724
    formatContainedPermissions(roles) {
gj4210's avatar
gj4210 committed
725
      roles.forEach(role => {
janis.streib's avatar
janis.streib committed
726
727
728
        if (role.contained_permissions) {
          const permissions = []
          for (const [key, value] of Object.entries(role.contained_permissions)) {
gj4210's avatar
gj4210 committed
729
            permissions.push({system: key, permission: value})
gj4210's avatar
gj4210 committed
730
          }
janis.streib's avatar
janis.streib committed
731
732
733
          role.contained_permissions = permissions
        }
      }
gj4210's avatar
gj4210 committed
734
735
      )
      return roles
gj4210's avatar
gj4210 committed
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
    },
    addRole(e) {
      e.preventDefault()
      const new_role = this.new_system + '.' + this.new_role_name
      this.new_account.roles.push(this.assignable_roles_by_fq_name[new_role][0])
      this.updateCanAddRole()
    },
    removeRole(role) {
      const index = this.new_account.roles.indexOf(role)
      if (index > -1) {
        this.new_account.roles.splice(index, 1)
      }
      this.updateCanAddRole()
    },
    updateCanAddRole() {
gj4210's avatar
gj4210 committed
751
752
      const new_role = this.new_system + '.' + this.new_role_name
      if (!this.new_system || !this.new_role_name || !this.assignable_roles_by_fq_name[new_role]) {
gj4210's avatar
gj4210 committed
753
        this.can_add_role = false
754
        this.cant_add_role_reason = 'System & Rolle auswählen'
gj4210's avatar
gj4210 committed
755
756
        return
      }
757
      let valid = true
gj4210's avatar
gj4210 committed
758
      this.new_account.roles.forEach(role => {
759
760
761
762
763
764
765
766
767
        if (role.fq_name === new_role) {
          valid = false
          this.cant_add_role_reason = 'Rolle schon hinzugefügt'
        } else if (role.contained_roles.includes(new_role)) {
          valid = false
          this.cant_add_role_reason = role.fq_name + ' enthält ' + new_role
        } else if (this.assignable_roles_by_fq_name[new_role][0].contained_roles.includes(role.fq_name)) {
          valid = false
          this.cant_add_role_reason = new_role + ' enthält ' + role.fq_name
gj4210's avatar
gj4210 committed
768
769
        }
      })
770
771
772
773
774

      this.can_add_role = valid
      if (valid) {
        this.cant_add_role_reason = ''
      }
janis.streib's avatar
janis.streib committed
775
    }
gj4210's avatar
gj4210 committed
776
777
  }
}
gj4210's avatar
gj4210 committed
778
779
780
781
782
783
784
785
786
787
788
</script>

<style>
.collapse-icon {
  transition: transform;
  transition-duration: 250ms;
}

.not-collapsed > .collapse-icon {
  transform: rotate(-180deg);
}
gj4210's avatar
gj4210 committed
789
790
791
792
793
794

.b-form-tag {
  font-size: 100%;
  font-weight: normal;
  line-height: 1.5;
}
gj4210's avatar
gj4210 committed
795
</style>