Tokens.vue 29.7 KB
Newer Older
Janis Streib's avatar
Janis Streib committed
1
<template>
Robert's avatar
Robert 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"
Robert's avatar
Robert 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
Robert's avatar
Robert committed
9
10
11
            id="input-account-create-description"
            v-model.trim="new_account.description"
            placeholder="Beschreibung eingeben"
Robert's avatar
Robert committed
12
13
          />
        </b-form-group>
Robert's avatar
Robert 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">
Robert's avatar
Robert 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>
Robert's avatar
Robert committed
19
            <b-form-radio v-model="new_account.do_copy_roles" :value="false" class="mb-2">individuell zuweisen
Robert's avatar
Robert committed
20
            </b-form-radio>
Robert's avatar
Robert 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>
Robert's avatar
Robert 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>
Robert's avatar
Robert 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
                  </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>
59
60
                <b-button v-if="new_account.roles.length > 0" variant="outline-danger" block class="mt-1"
                          @click="removeAllRoles">
Robert's avatar
Robert committed
61
62
                  Alle Rollen entfernen
                </b-button>
Robert's avatar
Robert committed
63
              </h5>
Robert's avatar
Robert committed
64
            </template>
Robert's avatar
Robert committed
65
          </b-form-group>
Robert's avatar
Robert committed
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
        </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
91
          Unterkonto erstellen
Robert's avatar
Robert committed
92
93
94
95
96
97
98
99
        </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
Robert's avatar
Robert committed
100
101
102
            id="input-account-edit-description"
            v-model.trim="new_account.description"
            placeholder="Beschreibung eingeben"
Robert's avatar
Robert committed
103
104
          />
        </b-form-group>
Robert's avatar
Robert committed
105
106
        <template
          v-if="roles_by_account != null && roles_by_account[$store.state.user.login_name] != null && assignable_role_name_options_by_system != null">
Robert's avatar
Robert committed
107
          <b-form-group label="Rollen">
Robert's avatar
Robert committed
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'}"
127
                  class="mr-1 mb-1 badge" tabindex="0" href="#">
Robert's avatar
Robert committed
128
129
130
                  <div class="d-flex align-items-center">
                    {{ role.system.toUpperCase() }}<br>{{ role.name }}
                    <span @click="removeRole(role)" class="p-2">
Robert's avatar
Robert committed
131
132
                      <font-awesome-icon style="opacity: 0.7" :icon="['fas','times']"/>
                        </span>
Robert's avatar
Robert committed
133
134
135
136
137
138
139
140
141
142
143
144
                  </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>
145
146
              <b-button v-if="new_account.roles.length > 0" variant="outline-danger" block class="mt-1"
                        @click="removeAllRoles">
Robert's avatar
Robert committed
147
148
                Alle Rollen entfernen
              </b-button>
Robert's avatar
Robert committed
149
            </h5>
Robert's avatar
Robert committed
150
151
          </b-form-group>
        </template>
Robert's avatar
Robert committed
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
      </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
Robert's avatar
Robert committed
184
185
186
187
            id="input-token-create-description"
            v-model.trim="new_token.description"
            required
            placeholder="Beschreibung"
Robert's avatar
Robert committed
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
213
214
          />
        </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">
Robert's avatar
Robert committed
215
      <CopyField class="shadow" :text="token"/>
Robert's avatar
Robert committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
      <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>
    <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>
240
                    </template>
Robert's avatar
Robert committed
241
242
243
244
245
246
247
248
249
                    <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.
250
                </div>
Robert's avatar
Robert committed
251
252
253
254
255
256
257
                <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>
Robert's avatar
Robert committed
258
                <template v-if="roles_by_account[account.login_name] != null && assignable_roles_by_fq_name != null">
Robert's avatar
Robert committed
259
260
                  <h5 class="mb-0">
                    <template
Robert's avatar
Robert committed
261
                      v-for="role in roles_by_account[account.login_name].slice(0, max_role_badge_count)">
Robert's avatar
Robert committed
262
                      <b-button
Robert's avatar
Robert committed
263
264
265
                        :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'}"
Janis Streib's avatar
Janis Streib committed
266
                        class="mr-1 mb-1 badge" tabindex="0" href="#">{{ role.system.toUpperCase() }}<br>{{ role.role }}
Robert's avatar
Robert committed
267
268
                      </b-button>
                      <b-popover
Robert's avatar
Robert committed
269
270
271
272
                        :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">
Robert's avatar
Robert committed
273
                        <b-table :fields="permission_list_fields"
Robert's avatar
Robert committed
274
                                 :items="assignable_roles_by_fq_name[role.role_fq_name][0].contained_permissions"
Robert's avatar
Robert committed
275
276
                                 sticky-header/>
                      </b-popover>
277
                    </template>
Robert's avatar
Robert committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
                  </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">
Janis Streib's avatar
Janis Streib committed
295
                    Konto bearbeiten
Robert's avatar
Robert committed
296
297
298
299
300
301
                  </b-tooltip>
                </template>
              </b-col>
            </b-row>
          </template>
          <b-button block squared variant="outline-secondary" v-b-toggle="account.login_name + '-collapse'">
Janis Streib's avatar
Janis Streib committed
302
            Tokens<br/>
Robert's avatar
Robert committed
303
            <font-awesome-icon class="collapse-icon" :icon="['fas','chevron-down']"/>
Robert's avatar
Robert committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
          </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>
319
                  <b-tooltip :target="'button-create-token-' +  account.login_name" triggers="hover"
Robert's avatar
Robert committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
                             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>
347
      <DBEditor object_fq_name="cntl.wapi_auth" modal_id="token_editor" :object_function="db_editor_function"
348
349
                :old_data="db_editor_old_data"
                :presets="db_editor_presets"
350
                :non_optionals_order="['login_name', 'description', 'expiration_date', 'do_refresh_token']"
351
      ></DBEditor>
Robert's avatar
Robert committed
352
353
    </Loading>
  </div>
Janis Streib's avatar
Janis Streib committed
354
355
356
</template>

<script>
Janis Streib's avatar
Janis Streib committed
357
import AccountTokenService from '@/api-services/account_token.service'
Robert's avatar
Robert committed
358
359
360
361
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
362
import DateTimePicker from '../../components/DateTimePicker'
Robert's avatar
Robert committed
363
364
import Mgr2RoleService from '@/api-services.gen/cntl.mgr2role'
import RoleService from '@/api-services.gen/cntl.role'
Janis Streib's avatar
Janis Streib committed
365
366
import Loading from '../../components/Loading'
import CopyField from '../../components/CopyField'
367
import DBEditor from '@/components/DBEditor'
Janis Streib's avatar
Janis Streib committed
368

Robert's avatar
Robert committed
369
370
export default {
  name: 'tokens',
371
  components: {DBEditor, CopyField, Loading, DateTimePicker},
Robert's avatar
Robert committed
372
  data() {
Robert's avatar
Robert committed
373
    return {
374
375
      db_editor_old_data: null,
      db_editor_presets: null,
376
      db_editor_function: 'update',
Robert's avatar
Robert committed
377
378
      tokens_by_account: null,
      roles_by_account: null,
Robert's avatar
Robert committed
379
380
381
382
383
      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,
Robert's avatar
Robert committed
384
385
386
387
388
389
390
      accounts: null,
      new_account: {
        parent_login_name: null,
        description: '',
        login_name: '',
        expiration_date: null,
        do_copy_roles: true,
Robert's avatar
Robert committed
391
        roles: []
Robert's avatar
Robert committed
392
393
394
395
396
397
      },
      new_token: {
        description: '',
        login_name: '',
        expiration_date: null
      },
Janis Streib's avatar
Janis Streib committed
398
      filter_text: '',
Robert's avatar
Robert committed
399
400
401
      token_list_fields: [
        {
          key: 'description',
Janis Streib's avatar
Janis Streib committed
402
          label: 'Beschreibung',
Robert's avatar
Robert committed
403
          sortable: false
Janis Streib's avatar
Janis Streib committed
404
        },
Robert's avatar
Robert committed
405
406
        {
          key: 'expiration_date',
Janis Streib's avatar
Janis Streib committed
407
          label: 'Ablaufdatum',
Robert's avatar
Robert committed
408
          sortable: true
409
        },
Robert's avatar
Robert committed
410
411
        {
          key: 'last_login_date',
Janis Streib's avatar
Janis Streib committed
412
          label: 'Zuletzt verwendet',
Robert's avatar
Robert committed
413
414
          formatter: this.formatDate,
          sortable: true
415
        },
Robert's avatar
Robert committed
416
417
        {
          key: 'last_generate_date',
Janis Streib's avatar
Janis Streib committed
418
          label: 'Zuletzt generiert',
Robert's avatar
Robert committed
419
420
421
422
423
424
425
          formatter: this.formatDate,
          sortable: true
        },
        {
          key: 'buttons',
          label: '',
          sortable: false
Janis Streib's avatar
Janis Streib committed
426
        }
Robert's avatar
Robert committed
427
428
429
430
      ],
      permission_list_fields: [
        {
          key: 'system',
Janis Streib's avatar
Janis Streib committed
431
          label: 'System'
Robert's avatar
Robert committed
432
433
434
435
        },
        {
          key: 'permission',
          label: 'Berechtigung'
Janis Streib's avatar
Janis Streib committed
436
        }
Robert's avatar
Robert committed
437
438
      ],
      show_modal_alert: true,
Janis Streib's avatar
Janis Streib committed
439
      modal_alert_content: '',
Robert's avatar
Robert committed
440
      waiting: false,
Janis Streib's avatar
Janis Streib committed
441
      token: '',
Robert's avatar
Robert committed
442
      max_role_badge_count: 10,
Robert's avatar
Robert committed
443
444
445
      max_permission_count: 10,
      new_system: '',
      new_role_name: '',
446
447
      can_add_role: false,
      cant_add_role_reason: ''
Robert's avatar
Robert committed
448
449
450
    }
  },
  computed: {
Robert's avatar
Robert committed
451
    filtered_accounts() {
Robert's avatar
Robert committed
452
453
454
455
456
457
458
      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
459
        return account.login_name.toLowerCase().includes(this.filter_text.toLowerCase()) ||
Robert's avatar
Robert committed
460
          (account.description != null && account.description.toLowerCase().includes(this.filter_text.toLowerCase()))
Robert's avatar
Robert committed
461
462
463
      })
    }
  },
Robert's avatar
Robert committed
464
  created() {
Robert's avatar
Robert committed
465
466
467
    this.fetchData()
  },
  methods: {
Robert's avatar
Robert committed
468
469
    fetchData() {
      AccountTokenService.list(this.$store.state.netdb_axios_config, this.$store.state.user.login_name).then(response => {
Robert's avatar
Robert committed
470
471
472
473
474
        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')
Robert's avatar
Robert committed
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
        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)
        })
Robert's avatar
Robert committed
501
502
      })
    },
Robert's avatar
Robert committed
503
    showModalEditAccount(account) {
504
505
      this.new_system = ''
      this.new_role_name = ''
Robert's avatar
Robert committed
506
507
508
      this.new_account.description = account.description
      this.new_account.login_name = account.login_name
      this.new_account.parent_login_name = account.parent_login_name
Robert's avatar
Robert committed
509
      this.new_account.roles = []
510
511
512
513
514
515
      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()
Robert's avatar
Robert committed
516
517
518
      this.show_modal_alert = false
      this.$bvModal.show('modal-edit-account')
    },
Robert's avatar
Robert committed
519
    showModalEditToken(token) {
520
      this.db_editor_old_data = this.db_editor_presets = token
521
522
      this.db_editor_function = 'update'
      this.$bvModal.show('token_editor')
Robert's avatar
Robert committed
523
    },
Robert's avatar
Robert committed
524
    showModalCreateToken(login_name) {
525
526
527
528
      this.db_editor_old_data = {}
      this.db_editor_presets = { login_name: login_name }
      this.db_editor_function = 'create'
      this.$bvModal.show('token_editor')
Robert's avatar
Robert committed
529
    },
Robert's avatar
Robert committed
530
    resetAccountData() {
Robert's avatar
Robert committed
531
532
533
534
535
536
      this.new_account = {
        parent_login_name: null,
        description: '',
        login_name: '',
        expiration_date: null,
        do_copy_roles: true,
Robert's avatar
Robert committed
537
        roles: []
Robert's avatar
Robert committed
538
      }
Robert's avatar
Robert committed
539
540
      this.new_system = ''
      this.new_role_name = ''
Robert's avatar
Robert committed
541
    },
Robert's avatar
Robert committed
542
    resetTokenData() {
Robert's avatar
Robert committed
543
544
545
546
547
548
      this.new_token = {
        description: '',
        login_name: '',
        expiration_date: null
      }
    },
Robert's avatar
Robert committed
549
    createAccount() {
Robert's avatar
Robert committed
550
551
552
553
554
555
556
557
558
      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
559
          const roles = []
Robert's avatar
Robert committed
560
561
          this.new_account.roles.forEach(role => {
            roles.push(role.fq_name)
Robert's avatar
Robert committed
562
          })
Robert's avatar
Robert committed
563
          Mgr2RoleService.bulk_update(this.$store.state.netdb_axios_config, {
Robert's avatar
Robert committed
564
565
566
567
            mgr_login_name_old: this.new_account.login_name,
            role_fq_name_list_old: [],
            role_fq_name_list_new: roles
          }).then(() => {
Robert's avatar
Robert committed
568
569
            this.$bvModal.hide('modal-create-account')
            this.fetchData()
Robert's avatar
Robert committed
570
571
572
573
574
575
576
577
578
            this.waiting = false
          })
        }
      }).catch(error => {
        this.modal_alert_content = error.response.data.error.type.text_descr
        this.show_modal_alert = true
        this.waiting = false
      })
    },
Robert's avatar
Robert committed
579
    editAccount() {
Robert's avatar
Robert committed
580
      this.waiting = true
Robert's avatar
Robert committed
581
      const old_roles = []
Robert's avatar
Robert committed
582
583
584
585
586
      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)
        })
      }
Robert's avatar
Robert committed
587
588
589
590
591
592
      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))
Robert's avatar
Robert committed
593
594
595
596
597
      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,
Robert's avatar
Robert committed
598
599
        roles_to_add: roles_to_add,
        roles_to_remove: roles_to_remove
Robert's avatar
Robert committed
600
601
602
603
604
605
606
607
608
609
      }).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
      })
    },
Robert's avatar
Robert committed
610
    deleteAccount() {
Robert's avatar
Robert committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
      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
      })
    },
Robert's avatar
Robert committed
625
    createToken() {
Robert's avatar
Robert committed
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
      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
      })
    },
Robert's avatar
Robert committed
643
    deleteToken() {
Robert's avatar
Robert committed
644
645
646
647
648
649
650
651
652
653
654
655
656
      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
      })
    },
Robert's avatar
Robert committed
657
    formatDate(value) {
Robert's avatar
Robert committed
658
659
660
661
662
      if (value == null) {
        return 'N/A'
      }
      return new Date(Date.parse(value)).toLocaleString('de-DE')
    },
Robert's avatar
Robert committed
663
    getDate30DaysAhead() {
Janis Streib's avatar
Janis Streib committed
664
      const d = new Date()
Robert's avatar
Robert committed
665
666
667
      d.setDate(d.getDate() + 29)
      return d
    },
Robert's avatar
Robert committed
668
    formatExpiredTokens(tokens) {
Robert's avatar
Robert committed
669
      tokens.forEach(token => {
Janis Streib's avatar
Janis Streib committed
670
671
672
        if (token.is_expired) {
          token._rowVariant = 'danger'
        }
673
      })
Robert's avatar
Robert committed
674
675
      return tokens
    },
Robert's avatar
Robert committed
676
    formatContainedPermissions(roles) {
Robert's avatar
Robert committed
677
      roles.forEach(role => {
Janis Streib's avatar
Janis Streib committed
678
679
680
        if (role.contained_permissions) {
          const permissions = []
          for (const [key, value] of Object.entries(role.contained_permissions)) {
Robert's avatar
Robert committed
681
            permissions.push({system: key, permission: value})
Robert's avatar
Robert committed
682
          }
Janis Streib's avatar
Janis Streib committed
683
684
          role.contained_permissions = permissions
        }
685
      })
Robert's avatar
Robert committed
686
      return roles
Robert's avatar
Robert committed
687
688
689
690
691
692
693
694
695
696
697
698
699
700
    },
    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()
    },
Robert's avatar
Robert committed
701
702
703
704
    removeAllRoles() {
      this.new_account.roles = []
      this.updateCanAddRole()
    },
Robert's avatar
Robert committed
705
    updateCanAddRole() {
Robert's avatar
Robert committed
706
707
      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]) {
Robert's avatar
Robert committed
708
        this.can_add_role = false
709
        this.cant_add_role_reason = 'System & Rolle auswählen'
Robert's avatar
Robert committed
710
711
        return
      }
712
      let valid = true
Robert's avatar
Robert committed
713
      this.new_account.roles.forEach(role => {
714
715
716
717
718
719
720
721
722
        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
Robert's avatar
Robert committed
723
724
        }
      })
725
726
727
728
729

      this.can_add_role = valid
      if (valid) {
        this.cant_add_role_reason = ''
      }
Janis Streib's avatar
Janis Streib committed
730
    }
Robert's avatar
Robert committed
731
732
  }
}
Robert's avatar
Robert committed
733
734
735
</script>

<style>
Robert's avatar
Robert committed
736
737
738
739
740
.b-form-tag {
  font-size: 100%;
  font-weight: normal;
  line-height: 1.5;
}
Robert's avatar
Robert committed
741
</style>