Merge pull request #4993 from vvlladd28/improvement/mail-setting/edit-password

[3.3.0] Mail server settings improvement change password
This commit is contained in:
Igor Kulikov 2021-08-04 18:37:43 +03:00 committed by GitHub
commit 017dde8c55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 18 deletions

View File

@ -26,12 +26,12 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.thingsboard.rule.engine.api.MailService; import org.thingsboard.rule.engine.api.MailService;
import org.thingsboard.rule.engine.api.SmsService; import org.thingsboard.rule.engine.api.SmsService;
import org.thingsboard.server.common.data.sms.config.TestSmsRequest;
import org.thingsboard.server.common.data.AdminSettings; import org.thingsboard.server.common.data.AdminSettings;
import org.thingsboard.server.common.data.UpdateMessage; import org.thingsboard.server.common.data.UpdateMessage;
import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.security.model.SecuritySettings; import org.thingsboard.server.common.data.security.model.SecuritySettings;
import org.thingsboard.server.common.data.sms.config.TestSmsRequest;
import org.thingsboard.server.dao.settings.AdminSettingsService; import org.thingsboard.server.dao.settings.AdminSettingsService;
import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.security.permission.Operation; import org.thingsboard.server.service.security.permission.Operation;
@ -67,7 +67,7 @@ public class AdminController extends BaseController {
accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.READ); accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.READ);
AdminSettings adminSettings = checkNotNull(adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, key)); AdminSettings adminSettings = checkNotNull(adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, key));
if (adminSettings.getKey().equals("mail")) { if (adminSettings.getKey().equals("mail")) {
((ObjectNode) adminSettings.getJsonValue()).put("password", ""); ((ObjectNode) adminSettings.getJsonValue()).remove("password");
} }
return adminSettings; return adminSettings;
} catch (Exception e) { } catch (Exception e) {
@ -84,7 +84,7 @@ public class AdminController extends BaseController {
adminSettings = checkNotNull(adminSettingsService.saveAdminSettings(TenantId.SYS_TENANT_ID, adminSettings)); adminSettings = checkNotNull(adminSettingsService.saveAdminSettings(TenantId.SYS_TENANT_ID, adminSettings));
if (adminSettings.getKey().equals("mail")) { if (adminSettings.getKey().equals("mail")) {
mailService.updateMailConfiguration(); mailService.updateMailConfiguration();
((ObjectNode) adminSettings.getJsonValue()).put("password", ""); ((ObjectNode) adminSettings.getJsonValue()).remove("password");
} else if (adminSettings.getKey().equals("sms")) { } else if (adminSettings.getKey().equals("sms")) {
smsService.updateSmsConfiguration(); smsService.updateSmsConfiguration();
} }
@ -126,6 +126,10 @@ public class AdminController extends BaseController {
accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.READ); accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.READ);
adminSettings = checkNotNull(adminSettings); adminSettings = checkNotNull(adminSettings);
if (adminSettings.getKey().equals("mail")) { if (adminSettings.getKey().equals("mail")) {
if(!adminSettings.getJsonValue().has("password")) {
AdminSettings mailSettings = checkNotNull(adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "mail"));
((ObjectNode) adminSettings.getJsonValue()).put("password", mailSettings.getJsonValue().get("password").asText());
}
String email = getCurrentUser().getEmail(); String email = getCurrentUser().getEmail();
mailService.sendTestMail(adminSettings.getJsonValue(), email); mailService.sendTestMail(adminSettings.getJsonValue(), email);
} }

View File

@ -215,6 +215,7 @@ public class DefaultSystemDataLoaderService implements SystemDataLoaderService {
node.put("password", ""); node.put("password", "");
node.put("tlsVersion", "TLSv1.2");//NOSONAR, key used to identify password field (not password value itself) node.put("tlsVersion", "TLSv1.2");//NOSONAR, key used to identify password field (not password value itself)
node.put("enableProxy", false); node.put("enableProxy", false);
node.put("showChangePassword", false);
mailSettings.setJsonValue(node); mailSettings.setJsonValue(node);
adminSettingsService.saveAdminSettings(TenantId.SYS_TENANT_ID, mailSettings); adminSettingsService.saveAdminSettings(TenantId.SYS_TENANT_ID, mailSettings);
} }

View File

@ -52,7 +52,7 @@ public class AdminSettingsServiceImpl implements AdminSettingsService {
public AdminSettings saveAdminSettings(TenantId tenantId, AdminSettings adminSettings) { public AdminSettings saveAdminSettings(TenantId tenantId, AdminSettings adminSettings) {
log.trace("Executing saveAdminSettings [{}]", adminSettings); log.trace("Executing saveAdminSettings [{}]", adminSettings);
adminSettingsValidator.validate(adminSettings, data -> tenantId); adminSettingsValidator.validate(adminSettings, data -> tenantId);
if (adminSettings.getKey().equals("mail") && "".equals(adminSettings.getJsonValue().get("password").asText())) { if(adminSettings.getKey().equals("mail") && !adminSettings.getJsonValue().has("password")) {
AdminSettings mailSettings = findAdminSettingsByKey(tenantId, "mail"); AdminSettings mailSettings = findAdminSettingsByKey(tenantId, "mail");
if (mailSettings != null) { if (mailSettings != null) {
((ObjectNode) adminSettings.getJsonValue()).put("password", mailSettings.getJsonValue().get("password").asText()); ((ObjectNode) adminSettings.getJsonValue()).put("password", mailSettings.getJsonValue().get("password").asText());

View File

@ -39,7 +39,7 @@
</mat-form-field> </mat-form-field>
<mat-form-field class="mat-block"> <mat-form-field class="mat-block">
<mat-label translate>admin.smtp-protocol</mat-label> <mat-label translate>admin.smtp-protocol</mat-label>
<mat-select matInput formControlName="smtpProtocol"> <mat-select formControlName="smtpProtocol">
<mat-option *ngFor="let protocol of smtpProtocols" [value]="protocol"> <mat-option *ngFor="let protocol of smtpProtocols" [value]="protocol">
{{protocol.toUpperCase()}} {{protocol.toUpperCase()}}
</mat-option> </mat-option>
@ -126,10 +126,13 @@
<input matInput formControlName="username" placeholder="{{ 'common.enter-username' | translate }}" <input matInput formControlName="username" placeholder="{{ 'common.enter-username' | translate }}"
autocomplete="new-username"/> autocomplete="new-username"/>
</mat-form-field> </mat-form-field>
<mat-form-field class="mat-block"> <mat-checkbox *ngIf="showChangePassword" formControlName="changePassword" style="padding-bottom: 16px;">
{{ 'admin.change-password' | translate }}
</mat-checkbox>
<mat-form-field class="mat-block" *ngIf="mailSettings.get('changePassword').value || !showChangePassword">
<mat-label translate>common.password</mat-label> <mat-label translate>common.password</mat-label>
<input matInput formControlName="password" type="password" <input matInput formControlName="password" type="password"
placeholder="{{ 'common.enter-password' | translate }}" autocomplete="new-password"/> placeholder="{{ 'common.enter-password' | translate }}" autocomplete="new-password">
</mat-form-field> </mat-form-field>
<div fxLayout="row" fxLayoutAlign="end center" fxLayout.xs="column" fxLayoutAlign.xs="end" fxLayoutGap="16px"> <div fxLayout="row" fxLayoutAlign="end center" fxLayout.xs="column" fxLayoutAlign.xs="end" fxLayoutGap="16px">
<button mat-raised-button type="button" <button mat-raised-button type="button"

View File

@ -14,7 +14,7 @@
/// limitations under the License. /// limitations under the License.
/// ///
import { Component, OnInit } from '@angular/core'; import { Component, OnDestroy, OnInit } from '@angular/core';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state'; import { AppState } from '@core/core.state';
import { PageComponent } from '@shared/components/page.component'; import { PageComponent } from '@shared/components/page.component';
@ -25,21 +25,26 @@ import { AdminService } from '@core/http/admin.service';
import { ActionNotificationShow } from '@core/notification/notification.actions'; import { ActionNotificationShow } from '@core/notification/notification.actions';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { HasConfirmForm } from '@core/guards/confirm-on-exit.guard'; import { HasConfirmForm } from '@core/guards/confirm-on-exit.guard';
import { isString } from '@core/utils'; import { isDefinedAndNotNull, isString } from '@core/utils';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
@Component({ @Component({
selector: 'tb-mail-server', selector: 'tb-mail-server',
templateUrl: './mail-server.component.html', templateUrl: './mail-server.component.html',
styleUrls: ['./mail-server.component.scss', './settings-card.scss'] styleUrls: ['./mail-server.component.scss', './settings-card.scss']
}) })
export class MailServerComponent extends PageComponent implements OnInit, HasConfirmForm { export class MailServerComponent extends PageComponent implements OnInit, OnDestroy, HasConfirmForm {
mailSettings: FormGroup; mailSettings: FormGroup;
adminSettings: AdminSettings<MailServerSettings>; adminSettings: AdminSettings<MailServerSettings>;
smtpProtocols = ['smtp', 'smtps']; smtpProtocols = ['smtp', 'smtps'];
showChangePassword = false;
tlsVersions = ['TLSv1', 'TLSv1.1', 'TLSv1.2', 'TLSv1.3']; tlsVersions = ['TLSv1', 'TLSv1.1', 'TLSv1.2', 'TLSv1.3'];
private destroy$ = new Subject();
constructor(protected store: Store<AppState>, constructor(protected store: Store<AppState>,
private router: Router, private router: Router,
private adminService: AdminService, private adminService: AdminService,
@ -56,12 +61,22 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon
if (this.adminSettings.jsonValue && isString(this.adminSettings.jsonValue.enableTls)) { if (this.adminSettings.jsonValue && isString(this.adminSettings.jsonValue.enableTls)) {
this.adminSettings.jsonValue.enableTls = (this.adminSettings.jsonValue.enableTls as any) === 'true'; this.adminSettings.jsonValue.enableTls = (this.adminSettings.jsonValue.enableTls as any) === 'true';
} }
this.showChangePassword =
isDefinedAndNotNull(this.adminSettings.jsonValue.showChangePassword) ? this.adminSettings.jsonValue.showChangePassword : true ;
delete this.adminSettings.jsonValue.showChangePassword;
this.mailSettings.reset(this.adminSettings.jsonValue); this.mailSettings.reset(this.adminSettings.jsonValue);
this.enableMailPassword(!this.showChangePassword);
this.enableProxyChanged(); this.enableProxyChanged();
} }
); );
} }
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
super.ngOnDestroy();
}
buildMailServerSettingsForm() { buildMailServerSettingsForm() {
this.mailSettings = this.fb.group({ this.mailSettings = this.fb.group({
mailFrom: ['', [Validators.required]], mailFrom: ['', [Validators.required]],
@ -81,14 +96,23 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon
proxyUser: [''], proxyUser: [''],
proxyPassword: [''], proxyPassword: [''],
username: [''], username: [''],
changePassword: [false],
password: [''] password: ['']
}); });
this.registerDisableOnLoadFormControl(this.mailSettings.get('smtpProtocol')); this.registerDisableOnLoadFormControl(this.mailSettings.get('smtpProtocol'));
this.registerDisableOnLoadFormControl(this.mailSettings.get('enableTls')); this.registerDisableOnLoadFormControl(this.mailSettings.get('enableTls'));
this.registerDisableOnLoadFormControl(this.mailSettings.get('enableProxy')); this.registerDisableOnLoadFormControl(this.mailSettings.get('enableProxy'));
this.mailSettings.get('enableProxy').valueChanges.subscribe(() => { this.registerDisableOnLoadFormControl(this.mailSettings.get('changePassword'));
this.mailSettings.get('enableProxy').valueChanges.pipe(
takeUntil(this.destroy$)
).subscribe(() => {
this.enableProxyChanged(); this.enableProxyChanged();
}); });
this.mailSettings.get('changePassword').valueChanges.pipe(
takeUntil(this.destroy$)
).subscribe((value) => {
this.enableMailPassword(value);
});
} }
enableProxyChanged(): void { enableProxyChanged(): void {
@ -102,8 +126,16 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon
} }
} }
enableMailPassword(enable: boolean) {
if (enable) {
this.mailSettings.get('password').enable({emitEvent: false});
} else {
this.mailSettings.get('password').disable({emitEvent: false});
}
}
sendTestMail(): void { sendTestMail(): void {
this.adminSettings.jsonValue = {...this.adminSettings.jsonValue, ...this.mailSettings.value}; this.adminSettings.jsonValue = {...this.adminSettings.jsonValue, ...this.mailSettingsFormValue};
this.adminService.sendTestMail(this.adminSettings).subscribe( this.adminService.sendTestMail(this.adminSettings).subscribe(
() => { () => {
this.store.dispatch(new ActionNotificationShow({ message: this.translate.instant('admin.test-mail-sent'), this.store.dispatch(new ActionNotificationShow({ message: this.translate.instant('admin.test-mail-sent'),
@ -113,13 +145,11 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon
} }
save(): void { save(): void {
this.adminSettings.jsonValue = {...this.adminSettings.jsonValue, ...this.mailSettings.value}; this.adminSettings.jsonValue = {...this.adminSettings.jsonValue, ...this.mailSettingsFormValue};
this.adminService.saveAdminSettings(this.adminSettings).subscribe( this.adminService.saveAdminSettings(this.adminSettings).subscribe(
(adminSettings) => { (adminSettings) => {
if (!adminSettings.jsonValue.password) {
adminSettings.jsonValue.password = this.mailSettings.value.password;
}
this.adminSettings = adminSettings; this.adminSettings = adminSettings;
this.showChangePassword = true;
this.mailSettings.reset(this.adminSettings.jsonValue); this.mailSettings.reset(this.adminSettings.jsonValue);
} }
); );
@ -129,4 +159,9 @@ export class MailServerComponent extends PageComponent implements OnInit, HasCon
return this.mailSettings; return this.mailSettings;
} }
private get mailSettingsFormValue(): MailServerSettings {
const formValue = this.mailSettings.value;
delete formValue.changePassword;
return formValue;
}
} }

View File

@ -27,6 +27,7 @@ export interface AdminSettings<T> {
export declare type SmtpProtocol = 'smtp' | 'smtps'; export declare type SmtpProtocol = 'smtp' | 'smtps';
export interface MailServerSettings { export interface MailServerSettings {
showChangePassword: boolean;
mailFrom: string; mailFrom: string;
smtpProtocol: SmtpProtocol; smtpProtocol: SmtpProtocol;
smtpHost: string; smtpHost: string;
@ -34,7 +35,8 @@ export interface MailServerSettings {
timeout: number; timeout: number;
enableTls: boolean; enableTls: boolean;
username: string; username: string;
password: string; changePassword?: boolean;
password?: string;
enableProxy: boolean; enableProxy: boolean;
proxyHost: string; proxyHost: string;
proxyPort: number; proxyPort: number;

View File

@ -104,6 +104,7 @@
"proxy-port-range": "Proxy port should be in a range from 1 to 65535.", "proxy-port-range": "Proxy port should be in a range from 1 to 65535.",
"proxy-user": "Proxy user", "proxy-user": "Proxy user",
"proxy-password": "Proxy password", "proxy-password": "Proxy password",
"change-password": "Change password",
"send-test-mail": "Send test mail", "send-test-mail": "Send test mail",
"sms-provider": "SMS provider", "sms-provider": "SMS provider",
"sms-provider-settings": "SMS provider settings", "sms-provider-settings": "SMS provider settings",