Commit 5d8d19282acc436df85de926151265ff5b0bf532

Authored by AndrewVolosytnykhThingsboard
2 parents f614a0a3 378c9c88

Merge branch 'improvement/mail-setting/edit-password' of https://github.com/vvlladd28/thingsboard

@@ -39,7 +39,7 @@ @@ -39,7 +39,7 @@
39 </mat-form-field> 39 </mat-form-field>
40 <mat-form-field class="mat-block"> 40 <mat-form-field class="mat-block">
41 <mat-label translate>admin.smtp-protocol</mat-label> 41 <mat-label translate>admin.smtp-protocol</mat-label>
42 - <mat-select matInput formControlName="smtpProtocol"> 42 + <mat-select formControlName="smtpProtocol">
43 <mat-option *ngFor="let protocol of smtpProtocols" [value]="protocol"> 43 <mat-option *ngFor="let protocol of smtpProtocols" [value]="protocol">
44 {{protocol.toUpperCase()}} 44 {{protocol.toUpperCase()}}
45 </mat-option> 45 </mat-option>
@@ -126,10 +126,13 @@ @@ -126,10 +126,13 @@
126 <input matInput formControlName="username" placeholder="{{ 'common.enter-username' | translate }}" 126 <input matInput formControlName="username" placeholder="{{ 'common.enter-username' | translate }}"
127 autocomplete="new-username"/> 127 autocomplete="new-username"/>
128 </mat-form-field> 128 </mat-form-field>
129 - <mat-form-field class="mat-block"> 129 + <mat-checkbox *ngIf="!isAdd" formControlName="changePassword" style="padding-bottom: 16px;">
  130 + {{ 'admin.change-password' | translate }}
  131 + </mat-checkbox>
  132 + <mat-form-field class="mat-block" *ngIf="mailSettings.get('changePassword').value || isAdd">
130 <mat-label translate>common.password</mat-label> 133 <mat-label translate>common.password</mat-label>
131 <input matInput formControlName="password" type="password" 134 <input matInput formControlName="password" type="password"
132 - placeholder="{{ 'common.enter-password' | translate }}" autocomplete="new-password"/> 135 + placeholder="{{ 'common.enter-password' | translate }}" autocomplete="new-password">
133 </mat-form-field> 136 </mat-form-field>
134 <div fxLayout="row" fxLayoutAlign="end center" fxLayout.xs="column" fxLayoutAlign.xs="end" fxLayoutGap="16px"> 137 <div fxLayout="row" fxLayoutAlign="end center" fxLayout.xs="column" fxLayoutAlign.xs="end" fxLayoutGap="16px">
135 <button mat-raised-button type="button" 138 <button mat-raised-button type="button"
@@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
14 /// limitations under the License. 14 /// limitations under the License.
15 /// 15 ///
16 16
17 -import { Component, OnInit } from '@angular/core'; 17 +import { Component, OnDestroy, OnInit } from '@angular/core';
18 import { Store } from '@ngrx/store'; 18 import { Store } from '@ngrx/store';
19 import { AppState } from '@core/core.state'; 19 import { AppState } from '@core/core.state';
20 import { PageComponent } from '@shared/components/page.component'; 20 import { PageComponent } from '@shared/components/page.component';
@@ -26,20 +26,25 @@ import { ActionNotificationShow } from '@core/notification/notification.actions' @@ -26,20 +26,25 @@ import { ActionNotificationShow } from '@core/notification/notification.actions'
26 import { TranslateService } from '@ngx-translate/core'; 26 import { TranslateService } from '@ngx-translate/core';
27 import { HasConfirmForm } from '@core/guards/confirm-on-exit.guard'; 27 import { HasConfirmForm } from '@core/guards/confirm-on-exit.guard';
28 import { isString } from '@core/utils'; 28 import { isString } from '@core/utils';
  29 +import { Subject } from 'rxjs';
  30 +import { takeUntil } from 'rxjs/operators';
29 31
30 @Component({ 32 @Component({
31 selector: 'tb-mail-server', 33 selector: 'tb-mail-server',
32 templateUrl: './mail-server.component.html', 34 templateUrl: './mail-server.component.html',
33 styleUrls: ['./mail-server.component.scss', './settings-card.scss'] 35 styleUrls: ['./mail-server.component.scss', './settings-card.scss']
34 }) 36 })
35 -export class MailServerComponent extends PageComponent implements OnInit, HasConfirmForm { 37 +export class MailServerComponent extends PageComponent implements OnInit, OnDestroy, HasConfirmForm {
36 38
37 mailSettings: FormGroup; 39 mailSettings: FormGroup;
38 adminSettings: AdminSettings<MailServerSettings>; 40 adminSettings: AdminSettings<MailServerSettings>;
39 smtpProtocols = ['smtp', 'smtps']; 41 smtpProtocols = ['smtp', 'smtps'];
  42 + isAdd = true;
40 43
41 tlsVersions = ['TLSv1', 'TLSv1.1', 'TLSv1.2', 'TLSv1.3']; 44 tlsVersions = ['TLSv1', 'TLSv1.1', 'TLSv1.2', 'TLSv1.3'];
42 45
  46 + private destroy$ = new Subject();
  47 +
43 constructor(protected store: Store<AppState>, 48 constructor(protected store: Store<AppState>,
44 private router: Router, 49 private router: Router,
45 private adminService: AdminService, 50 private adminService: AdminService,
@@ -56,12 +61,21 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon @@ -56,12 +61,21 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon
56 if (this.adminSettings.jsonValue && isString(this.adminSettings.jsonValue.enableTls)) { 61 if (this.adminSettings.jsonValue && isString(this.adminSettings.jsonValue.enableTls)) {
57 this.adminSettings.jsonValue.enableTls = (this.adminSettings.jsonValue.enableTls as any) === 'true'; 62 this.adminSettings.jsonValue.enableTls = (this.adminSettings.jsonValue.enableTls as any) === 'true';
58 } 63 }
  64 + this.isAdd = this.adminSettings.jsonValue.isAdd;
  65 + delete this.adminSettings.jsonValue.isAdd;
59 this.mailSettings.reset(this.adminSettings.jsonValue); 66 this.mailSettings.reset(this.adminSettings.jsonValue);
  67 + this.enableMailPassword(this.isAdd);
60 this.enableProxyChanged(); 68 this.enableProxyChanged();
61 } 69 }
62 ); 70 );
63 } 71 }
64 72
  73 + ngOnDestroy() {
  74 + this.destroy$.next();
  75 + this.destroy$.complete();
  76 + super.ngOnDestroy();
  77 + }
  78 +
65 buildMailServerSettingsForm() { 79 buildMailServerSettingsForm() {
66 this.mailSettings = this.fb.group({ 80 this.mailSettings = this.fb.group({
67 mailFrom: ['', [Validators.required]], 81 mailFrom: ['', [Validators.required]],
@@ -81,14 +95,23 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon @@ -81,14 +95,23 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon
81 proxyUser: [''], 95 proxyUser: [''],
82 proxyPassword: [''], 96 proxyPassword: [''],
83 username: [''], 97 username: [''],
  98 + changePassword: [false],
84 password: [''] 99 password: ['']
85 }); 100 });
86 this.registerDisableOnLoadFormControl(this.mailSettings.get('smtpProtocol')); 101 this.registerDisableOnLoadFormControl(this.mailSettings.get('smtpProtocol'));
87 this.registerDisableOnLoadFormControl(this.mailSettings.get('enableTls')); 102 this.registerDisableOnLoadFormControl(this.mailSettings.get('enableTls'));
88 this.registerDisableOnLoadFormControl(this.mailSettings.get('enableProxy')); 103 this.registerDisableOnLoadFormControl(this.mailSettings.get('enableProxy'));
89 - this.mailSettings.get('enableProxy').valueChanges.subscribe(() => { 104 + this.registerDisableOnLoadFormControl(this.mailSettings.get('changePassword'));
  105 + this.mailSettings.get('enableProxy').valueChanges.pipe(
  106 + takeUntil(this.destroy$)
  107 + ).subscribe(() => {
90 this.enableProxyChanged(); 108 this.enableProxyChanged();
91 }); 109 });
  110 + this.mailSettings.get('changePassword').valueChanges.pipe(
  111 + takeUntil(this.destroy$)
  112 + ).subscribe((value) => {
  113 + this.enableMailPassword(value);
  114 + });
92 } 115 }
93 116
94 enableProxyChanged(): void { 117 enableProxyChanged(): void {
@@ -102,8 +125,16 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon @@ -102,8 +125,16 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon
102 } 125 }
103 } 126 }
104 127
  128 + enableMailPassword(enable: boolean) {
  129 + if (enable) {
  130 + this.mailSettings.get('password').enable({emitEvent: false});
  131 + } else {
  132 + this.mailSettings.get('password').disable({emitEvent: false});
  133 + }
  134 + }
  135 +
105 sendTestMail(): void { 136 sendTestMail(): void {
106 - this.adminSettings.jsonValue = {...this.adminSettings.jsonValue, ...this.mailSettings.value}; 137 + this.adminSettings.jsonValue = {...this.adminSettings.jsonValue, ...this.mailSettingsFormValue};
107 this.adminService.sendTestMail(this.adminSettings).subscribe( 138 this.adminService.sendTestMail(this.adminSettings).subscribe(
108 () => { 139 () => {
109 this.store.dispatch(new ActionNotificationShow({ message: this.translate.instant('admin.test-mail-sent'), 140 this.store.dispatch(new ActionNotificationShow({ message: this.translate.instant('admin.test-mail-sent'),
@@ -113,12 +144,11 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon @@ -113,12 +144,11 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon
113 } 144 }
114 145
115 save(): void { 146 save(): void {
116 - this.adminSettings.jsonValue = {...this.adminSettings.jsonValue, ...this.mailSettings.value}; 147 + this.adminSettings.jsonValue = {...this.adminSettings.jsonValue, ...this.mailSettingsFormValue};
117 this.adminService.saveAdminSettings(this.adminSettings).subscribe( 148 this.adminService.saveAdminSettings(this.adminSettings).subscribe(
118 (adminSettings) => { 149 (adminSettings) => {
119 - if (!adminSettings.jsonValue.password) {  
120 - adminSettings.jsonValue.password = this.mailSettings.value.password;  
121 - } 150 + adminSettings.jsonValue.password = this.mailSettings.value.password;
  151 + adminSettings.jsonValue.changePassword = this.mailSettings.value.changePassword;
122 this.adminSettings = adminSettings; 152 this.adminSettings = adminSettings;
123 this.mailSettings.reset(this.adminSettings.jsonValue); 153 this.mailSettings.reset(this.adminSettings.jsonValue);
124 } 154 }
@@ -129,4 +159,9 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon @@ -129,4 +159,9 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon
129 return this.mailSettings; 159 return this.mailSettings;
130 } 160 }
131 161
  162 + private get mailSettingsFormValue(): MailServerSettings {
  163 + const formValue = this.mailSettings.value;
  164 + delete formValue.changePassword;
  165 + return formValue;
  166 + }
132 } 167 }
@@ -27,6 +27,7 @@ export interface AdminSettings<T> { @@ -27,6 +27,7 @@ export interface AdminSettings<T> {
27 export declare type SmtpProtocol = 'smtp' | 'smtps'; 27 export declare type SmtpProtocol = 'smtp' | 'smtps';
28 28
29 export interface MailServerSettings { 29 export interface MailServerSettings {
  30 + isAdd: boolean;
30 mailFrom: string; 31 mailFrom: string;
31 smtpProtocol: SmtpProtocol; 32 smtpProtocol: SmtpProtocol;
32 smtpHost: string; 33 smtpHost: string;
@@ -34,7 +35,8 @@ export interface MailServerSettings { @@ -34,7 +35,8 @@ export interface MailServerSettings {
34 timeout: number; 35 timeout: number;
35 enableTls: boolean; 36 enableTls: boolean;
36 username: string; 37 username: string;
37 - password: string; 38 + changePassword?: boolean;
  39 + password?: string;
38 enableProxy: boolean; 40 enableProxy: boolean;
39 proxyHost: string; 41 proxyHost: string;
40 proxyPort: number; 42 proxyPort: number;
@@ -104,6 +104,7 @@ @@ -104,6 +104,7 @@
104 "proxy-port-range": "Proxy port should be in a range from 1 to 65535.", 104 "proxy-port-range": "Proxy port should be in a range from 1 to 65535.",
105 "proxy-user": "Proxy user", 105 "proxy-user": "Proxy user",
106 "proxy-password": "Proxy password", 106 "proxy-password": "Proxy password",
  107 + "change-password": "Change password",
107 "send-test-mail": "Send test mail", 108 "send-test-mail": "Send test mail",
108 "sms-provider": "SMS provider", 109 "sms-provider": "SMS provider",
109 "sms-provider-settings": "SMS provider settings", 110 "sms-provider-settings": "SMS provider settings",