Projekt

Obecné

Profil

« Předchozí | Další » 

Revize b6006ff4

Přidáno uživatelem Jakub Hlaváč před téměř 4 roky(ů)

Re #8786 - Přidání uživatele - formulář, endpoint

+ OpenAPI definition for adding user
+ new popup form for adding user

Zobrazit rozdíly:

src/app/auth/interceptors/auth.interceptor.ts
28 28
      return of(null);
29 29
    }
30 30

  
31
    if (request.url.includes('DataService')){
31
    if (request.url.includes('DataService') || request.url.includes('GroupService')){
32 32
      request = request.clone({
33 33
        setParams: {
34 34
          user: localStorage.getItem(GlobalVariable.USER_NAME)
src/app/shared/api/endpoints/api.module.ts
5 5
import { ApiConfiguration, ApiConfigurationParams } from './api-configuration';
6 6

  
7 7
import { LoginService } from './services/login.service';
8
import { AdministrationService } from './services/administration.service';
8 9
import { GroupService } from './services/group.service';
9 10
import { DataService } from './services/data.service';
10 11
import { AnalyticsService } from './services/analytics.service';
......
20 21
  declarations: [],
21 22
  providers: [
22 23
    LoginService,
24
    AdministrationService,
23 25
    GroupService,
24 26
    DataService,
25 27
    AnalyticsService,
src/app/shared/api/endpoints/models.ts
1
export { Right } from './models/right';
1 2
export { InsertUnit } from './models/insert-unit';
2 3
export { InsertSensor } from './models/insert-sensor';
3 4
export { InsertPhenomenon } from './models/insert-phenomenon';
src/app/shared/api/endpoints/models/right.ts
1
/* tslint:disable */
2
/* eslint-disable */
3
export interface Right {
4
  note?: string;
5
  rightsId?: number;
6
  userRole?: string;
7
}
src/app/shared/api/endpoints/services.ts
1 1
export { LoginService } from './services/login.service';
2
export { AdministrationService } from './services/administration.service';
2 3
export { GroupService } from './services/group.service';
3 4
export { DataService } from './services/data.service';
4 5
export { AnalyticsService } from './services/analytics.service';
src/app/shared/api/endpoints/services/administration.service.ts
1
/* tslint:disable */
2
/* eslint-disable */
3
import { Injectable } from '@angular/core';
4
import { HttpClient, HttpResponse } from '@angular/common/http';
5
import { BaseService } from '../base-service';
6
import { ApiConfiguration } from '../api-configuration';
7
import { StrictHttpResponse } from '../strict-http-response';
8
import { RequestBuilder } from '../request-builder';
9
import { Observable } from 'rxjs';
10
import { map, filter } from 'rxjs/operators';
11

  
12
import { Right } from '../models/right';
13
import { UserInfo } from '../models/user-info';
14

  
15

  
16
/**
17
 * Administration service
18
 */
19
@Injectable({
20
  providedIn: 'root',
21
})
22
export class AdministrationService extends BaseService {
23
  constructor(
24
    config: ApiConfiguration,
25
    http: HttpClient
26
  ) {
27
    super(config, http);
28
  }
29

  
30
  /**
31
   * Path part for operation createUser
32
   */
33
  static readonly CreateUserPath = '/senslog15/rest/user';
34

  
35
  /**
36
   * Create User.
37
   *
38
   *
39
   *
40
   * This method provides access to the full `HttpResponse`, allowing access to response headers.
41
   * To access only the response body, use `createUser()` instead.
42
   *
43
   * This method sends `application/json` and handles request body of type `application/json`.
44
   */
45
  createUser$Response(params: {
46
    body: UserInfo
47
  }): Observable<StrictHttpResponse<void>> {
48

  
49
    const rb = new RequestBuilder(this.rootUrl, AdministrationService.CreateUserPath, 'post');
50
    if (params) {
51
      rb.body(params.body, 'application/json');
52
    }
53

  
54
    return this.http.request(rb.build({
55
      responseType: 'text',
56
      accept: '*/*'
57
    })).pipe(
58
      filter((r: any) => r instanceof HttpResponse),
59
      map((r: HttpResponse<any>) => {
60
        return (r as HttpResponse<any>).clone({ body: undefined }) as StrictHttpResponse<void>;
61
      })
62
    );
63
  }
64

  
65
  /**
66
   * Create User.
67
   *
68
   *
69
   *
70
   * This method provides access to only to the response body.
71
   * To access the full response (for headers, for example), `createUser$Response()` instead.
72
   *
73
   * This method sends `application/json` and handles request body of type `application/json`.
74
   */
75
  createUser(params: {
76
    body: UserInfo
77
  }): Observable<void> {
78

  
79
    return this.createUser$Response(params).pipe(
80
      map((r: StrictHttpResponse<void>) => r.body as void)
81
    );
82
  }
83

  
84
  /**
85
   * Path part for operation getRights
86
   */
87
  static readonly GetRightsPath = '/senslog15/rest/user/rights';
88

  
89
  /**
90
   * Get rights.
91
   *
92
   *
93
   *
94
   * This method provides access to the full `HttpResponse`, allowing access to response headers.
95
   * To access only the response body, use `getRights()` instead.
96
   *
97
   * This method doesn't expect any request body.
98
   */
99
  getRights$Response(params?: {
100
  }): Observable<StrictHttpResponse<Array<Right>>> {
101

  
102
    const rb = new RequestBuilder(this.rootUrl, AdministrationService.GetRightsPath, 'get');
103
    if (params) {
104
    }
105

  
106
    return this.http.request(rb.build({
107
      responseType: 'json',
108
      accept: 'application/json'
109
    })).pipe(
110
      filter((r: any) => r instanceof HttpResponse),
111
      map((r: HttpResponse<any>) => {
112
        return r as StrictHttpResponse<Array<Right>>;
113
      })
114
    );
115
  }
116

  
117
  /**
118
   * Get rights.
119
   *
120
   *
121
   *
122
   * This method provides access to only to the response body.
123
   * To access the full response (for headers, for example), `getRights$Response()` instead.
124
   *
125
   * This method doesn't expect any request body.
126
   */
127
  getRights(params?: {
128
  }): Observable<Array<Right>> {
129

  
130
    return this.getRights$Response(params).pipe(
131
      map((r: StrictHttpResponse<Array<Right>>) => r.body as Array<Right>)
132
    );
133
  }
134

  
135
}
src/app/shared/api/openapi.yaml
25 25
  description: Insert endpoints
26 26
- name: sensors
27 27
  description: Sensor service
28
- name: administration
29
  description: Administration service
28 30

  
29 31
paths:
30 32
  /senslog15/ControllerServlet:
......
65 67
            application/json:
66 68
              schema:
67 69
                $ref: '#/components/schemas/UserInfo'
70
    post:
71
      tags:
72
        - administration
73
      summary: Create User
74
      operationId: createUser
75
      responses:
76
        200:
77
          description: created
78
      requestBody:
79
        required: true
80
        content:
81
          application/json:
82
            schema:
83
              $ref: '#/components/schemas/UserInfo'
68 84

  
69 85
  /senslog1/GroupService:
70 86
    get:
......
306 322
                  type: array
307 323
                  items:
308 324
                    $ref: '#/components/schemas/InsertSensor'
325

  
326
  /senslog15/rest/user/rights:
327
    get:
328
      tags:
329
        - administration
330
      summary: Get rights
331
      operationId: getRights
332
      responses:
333
        200:
334
          description: success
335
          content:
336
            application/json:
337
              schema:
338
                type: array
339
                items:
340
                  $ref: '#/components/schemas/Right'
341

  
342

  
309 343
components:
310 344
  schemas:
345
    Right:
346
      type: object
347
      properties:
348
        rightsId:
349
          type: integer
350
        userRole:
351
          type: string
352
        note:
353
          type: string
354

  
311 355
    InsertUnit:
312 356
      type: object
313 357
      properties:
src/app/shared/nav-bar/components/nav-bar.component.html
11 11
              <a class="nav-link" [routerLink]="['/dashboard']"><h2>Dashboard</h2></a>
12 12
            </li>
13 13
            <li *ngIf="loggedUser?.userInfo?.rightsId == 0" class="nav-item">
14
              <a class="nav-link" [routerLink]="['/administration']"><h2>Administration</h2></a>
14
              <a class="nav-link" (click)="addUser()"><h2>Add user</h2></a>
15 15
            </li>
16 16
          </ul>
17 17
          <a class="navbar-brand" href="/">
......
31 31
    </nav>
32 32
  </div>
33 33
</div>
34

  
35
<app-user-insert-popup *ngIf="showAddUserPopup" [(isVisible)]="showAddUserPopup"></app-user-insert-popup>
src/app/shared/nav-bar/components/nav-bar.component.ts
2 2
import {AuthService} from '../../../auth/services/auth.service';
3 3
import {User} from '../../../auth/models/user';
4 4
import {Subscription} from 'rxjs';
5
import {Right} from '../../api/endpoints/models/right';
5 6

  
6 7
@Component({
7 8
  selector: 'app-nav-bar',
......
12 13

  
13 14
  loggedUser: User;
14 15
  subscription: Subscription[] = [];
16
  showAddUserPopup = false;
17
  rights: Right[];
15 18
  constructor(
16 19
    private authService: AuthService
17 20
  ) {
......
36 39
  ngOnDestroy(): void {
37 40
    this.subscription.forEach(subs => subs.unsubscribe());
38 41
  }
42

  
43
  addUser() {
44
    this.showAddUserPopup = true;
45
  }
39 46
}
src/app/shared/nav-bar/components/user-insert-popup/user-insert-popup.component.html
1
<p-dialog [visible]="isVisible" [modal]="true" [closable]="false" [draggable]="false" header="Add user"
2
          [style]="{width: '50vw'}" [baseZIndex]="10000">
3

  
4
  <form [formGroup]="insertForm">
5
    <div class="input-group form-group">
6
      <div class="input-group form-group">
7
        <div class="input-group-prepend">
8
          <span class="input-group-text">Username</span>
9
        </div>
10
        <input type="text" formControlName="userName" class="form-control" id="username" placeholder="Username"/>
11
      </div>
12
      <div class="input-group form-group">
13
        <div class="input-group-prepend">
14
          <span class="input-group-text">User password</span>
15
        </div>
16
        <input type="text" formControlName="userPass" class="form-control" id="password"
17
               placeholder="Password"/>
18
      </div>
19
      <div class="input-group form-group">
20
        <div class="input-group-prepend">
21
          <span class="input-group-text">User real name</span>
22
        </div>
23
        <input type="text" formControlName="userRealName" class="form-control" id="userRealName"
24
               placeholder="User real name"/>
25
      </div>
26
<!--      <div class="input-group form-group">
27
        <div class="input-group-prepend">
28
          <span class="input-group-text">Group</span>
29
        </div>
30
        <select formControlName="groups" id="groups">
31
          <option *ngFor="let group of groups; let i = index" [value]="groups[i].id">
32
            {{group[i]?.group_name}}
33
          </option>
34
        </select>
35
      </div>-->
36
      <div class="input-group form-group">
37
        <div class="input-group-prepend">
38
          <span class="input-group-text">Role</span>
39
        </div>
40
        <select formControlName="rights" id="rights">
41
          <option *ngFor="let right of rights; let i = index" [value]="rights[i].rightsId">
42
            {{rights[i].userRole}}
43
          </option>
44
        </select>
45
      </div>
46
    </div>
47
  </form>
48

  
49
  <p-footer>
50
    <div class="row justify-content-end align-items-center">
51
      <div>
52
        <p-button icon="pi pi-check" (click)="saveUser()" type="submit" label="Save" class="pr-2"></p-button>
53
        <p-button icon="pi pi-times" (click)="close()" label="Close" class="pr-2"></p-button>
54
      </div>
55
    </div>
56
  </p-footer>
57
</p-dialog>
src/app/shared/nav-bar/components/user-insert-popup/user-insert-popup.component.ts
1
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
2
import {map, tap} from 'rxjs/operators';
3
import {GroupService} from '../../../api/endpoints/services/group.service';
4
import {Group} from '../../../api/endpoints/models/group';
5
import {AdministrationService} from '../../../api/endpoints/services/administration.service';
6
import {Right} from '../../../api/endpoints/models/right';
7
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
8
import {HttpResponse} from '@angular/common/http';
9

  
10
@Component({
11
  selector: 'app-user-insert-popup',
12
  templateUrl: './user-insert-popup.component.html',
13
  styleUrls: ['./user-insert-popup.component.scss']
14
})
15
export class UserInsertPopupComponent implements OnInit {
16

  
17
  groups: Group[];
18
  rights: Right[];
19
  insertForm: FormGroup;
20
  @Input() isVisible;
21
  @Output() isVisibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
22

  
23
  constructor(
24
    private formBuilder: FormBuilder,
25
    private groupService: GroupService,
26
    private administrationService: AdministrationService
27
  ) {
28
    this.initForm();
29
    // TODO this.getGroups();
30
  }
31

  
32
  ngOnInit(): void {
33
  }
34

  
35
  getGroups() {
36
    this.groupService.getGroups({Operation: 'GetGroups'}).pipe(
37
      tap(data => this.groups = data)
38
    ).subscribe();
39
    console.log(this.groups);
40
  }
41

  
42
  getRights() {
43
    this.administrationService.getRights().pipe(
44
      tap(data => this.rights = data)
45
    ).subscribe();
46
  }
47

  
48
  close() {
49
    this.isVisibleChange.emit(false);
50
  }
51

  
52
  saveUser() {
53
    if (this.insertForm.valid) {
54
      this.administrationService.createUser$Response({body: {
55
        userPass: this.insertForm.controls.userPass.value,
56
        userName: this.insertForm.controls.userName.value,
57
        rightsId: this.insertForm.controls.rights.value,
58
        userRealName: this.insertForm.controls.userRealName.value,
59
          // TODO
60
        groupId: 2
61
        }
62
      }).pipe(
63
        map((response: HttpResponse<any>) => {
64
          if (response.status === 200) { // Accepts only 200 status response, due to API Definition
65
            this.close();
66
          } else {
67
            console.log('ERROR');
68
          }
69
        })
70
      ).toPromise();
71
    }
72
  }
73

  
74
  initForm() {
75
    this.getRights();
76
    this.insertForm = this.formBuilder.group({
77
      userName: ['', Validators.required],
78
      userPass: ['', Validators.required],
79
      userRealName: ['', Validators.required],
80
      // TODO groups: ['', Validators.required],
81
      rights: ['', Validators.required]
82
    });
83
  }
84
}
src/app/shared/nav-bar/nav-bar.module.ts
3 3
import {NavBarComponent} from './components/nav-bar.component';
4 4
import {RouterModule} from '@angular/router';
5 5
import {ButtonModule} from 'primeng/button';
6
import { UserInsertPopupComponent } from './components/user-insert-popup/user-insert-popup.component';
7
import {DialogModule} from 'primeng/dialog';
8
import {ReactiveFormsModule} from '@angular/forms';
6 9

  
7 10

  
8 11
@NgModule({
9
  declarations: [NavBarComponent],
12
  declarations: [NavBarComponent, UserInsertPopupComponent],
10 13
  exports: [
11 14
    NavBarComponent
12 15
  ],
13 16
  imports: [
14 17
    CommonModule,
15 18
    RouterModule,
16
    ButtonModule
19
    ButtonModule,
20
    DialogModule,
21
    ReactiveFormsModule
17 22
  ]
18 23
})
19 24
export class NavBarModule {

Také k dispozici: Unified diff