From 2009c862c453365d34a6a3bd4289d1f4eb436552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Thu, 28 Feb 2019 09:57:23 +0100 Subject: [PATCH] Fix client credentials user --- app/views/oauth/applications/_form.html.erb | 4 +- .../user-autocompleter.component.ts | 43 +++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/app/views/oauth/applications/_form.html.erb b/app/views/oauth/applications/_form.html.erb index e05792b574..706b72da3e 100644 --- a/app/views/oauth/applications/_form.html.erb +++ b/app/views/oauth/applications/_form.html.erb @@ -63,7 +63,9 @@ See docs/COPYRIGHT.rdoc for more details.
- + <%= f.hidden_field :client_credentials_user_id, value: @application.client_credentials_user_id %> +

diff --git a/frontend/src/app/modules/common/autocomplete/user-autocompleter.component.ts b/frontend/src/app/modules/common/autocomplete/user-autocompleter.component.ts index 8eca61d977..ea5140dbbb 100644 --- a/frontend/src/app/modules/common/autocomplete/user-autocompleter.component.ts +++ b/frontend/src/app/modules/common/autocomplete/user-autocompleter.component.ts @@ -26,23 +26,26 @@ // See doc/COPYRIGHT.rdoc for more details. // ++ -import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core'; +import {Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core'; import {DynamicBootstrapper} from "core-app/globals/dynamic-bootstrapper"; import {HalResourceService} from "core-app/modules/hal/services/hal-resource.service"; import {PathHelperService} from "core-app/modules/common/path-helper/path-helper.service"; import {ApiV3FilterBuilder} from "core-components/api/api-v3/api-v3-filter-builder"; import {NgSelectComponent} from "@ng-select/ng-select/dist"; +import {I18nService} from "core-app/modules/common/i18n/i18n.service"; @Component({ template: ` - @@ -59,14 +62,33 @@ export class UserAutocompleterComponent implements OnInit { // Load all users as default @Input() public url:string = this.pathHelper.api.v3.users.path; + @Input() public allowEmpty:boolean = false; + + @Input() public initialSelection:number|null = null; + + // Update an input field after changing, used when externally loaded + private updateInputField:HTMLInputElement|undefined; public options:any[]; - constructor(protected halResourceService:HalResourceService, + constructor(protected elementRef:ElementRef, + protected halResourceService:HalResourceService, + protected I18n:I18nService, readonly pathHelper:PathHelperService) { } ngOnInit() { + const input = this.elementRef.nativeElement.dataset['updateInput']; + const allowEmpty = this.elementRef.nativeElement.dataset['allowEmpty']; + if (input) { + this.updateInputField = document.getElementsByName(input)[0] as HTMLInputElement|undefined; + this.setInitialSelection(); + } + + if (allowEmpty === 'true') { + this.allowEmpty = true; + } + this.setAvailableUsers(this.url, ''); } @@ -77,6 +99,10 @@ export class UserAutocompleterComponent implements OnInit { if (this.clearAfterSelection) { this.ngSelectComponent.clearItem(user); } + + if (this.updateInputField) { + this.updateInputField.value = user.id; + } } } @@ -97,8 +123,19 @@ export class UserAutocompleterComponent implements OnInit { this.options = res.elements.map((el:any) => { return {name: el.name, id: el.id, href: el.href, avatar: el.avatar}; }); + + if (this.allowEmpty) { + this.options.unshift({ name: this.I18n.t('js.timelines.filter.noneSelection'), id: null }); + } }); } + + private setInitialSelection() { + if (this.updateInputField) { + const id = parseInt(this.updateInputField.value); + this.initialSelection = isNaN(id) ? null : id; + } + } } DynamicBootstrapper.register({ selector: 'user-autocompleter', cls: UserAutocompleterComponent });