Skip to content

Commit

Permalink
Password must be optional for Service Account #1808
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Dec 20, 2024
1 parent acb521a commit 975a63e
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/main/resources/apis/graphql/schema/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = schemaGenerator.createObjectType({
displayName: graphQl.nonNull(graphQl.GraphQLString),
email: graphQl.nonNull(graphQl.GraphQLString),
login: graphQl.nonNull(graphQl.GraphQLString),
password: graphQl.nonNull(graphQl.GraphQLString),
password: graphQl.GraphQLString,
memberships: graphQl.list(graphQl.GraphQLString)
},
resolve: function(env) {
Expand Down
39 changes: 29 additions & 10 deletions src/main/resources/assets/js/app/wizard/ChangeUserPasswordDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {Fieldset} from '@enonic/lib-admin-ui/ui/form/Fieldset';
import {Form} from '@enonic/lib-admin-ui/ui/form/Form';
import {showFeedback} from '@enonic/lib-admin-ui/notify/MessageBus';
import {Action} from '@enonic/lib-admin-ui/ui/Action';
import {SetUserPasswordEvent} from './SetUserPasswordEvent';

export class ChangeUserPasswordDialog
extends ModalDialog {
Expand Down Expand Up @@ -55,7 +56,19 @@ export class ChangeUserPasswordDialog

OpenChangePasswordDialogEvent.on((event) => {
this.principal = event.getPrincipal();
userPath.setHtml(this.principal.getKey().toPath());

if (!event.getPrincipal()) {
this.setHeading(i18n('dialog.setPassword.title'));
this.changePasswordAction.setLabel(i18n('action.setPassword'));
descMessage.setVisible(false);
userPath.setHtml('');
} else {
this.setHeading(i18n('dialog.changePassword.title'));
this.changePasswordAction.setLabel(i18n('action.changePassword'));
descMessage.setVisible(true);
userPath.setHtml(this.principal.getKey().toPath());
}

this.open();
});

Expand All @@ -69,15 +82,21 @@ export class ChangeUserPasswordDialog
if (!this.password.isValid()) {
return;
}
new SetUserPasswordRequest()
.setKey(this.principal.getKey())
.setPassword(this.password.getValue())
.sendAndParse()
.then(() => {
showFeedback(i18n('notify.change.password'));
this.close();
})
.catch(DefaultErrorHandler.handle);
if (this.principal) {
new SetUserPasswordRequest()
.setKey(this.principal.getKey())
.setPassword(this.password.getValue())
.sendAndParse()
.then(() => {
showFeedback(i18n('notify.change.password'));
this.close();
})
.catch(DefaultErrorHandler.handle);
} else {
new SetUserPasswordEvent(this.password.getValue()).fire();
showFeedback(i18n('notify.set.password'));
this.close();
}
});

this.addAction(this.changePasswordAction);
Expand Down
26 changes: 26 additions & 0 deletions src/main/resources/assets/js/app/wizard/SetUserPasswordEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {Event} from '@enonic/lib-admin-ui/event/Event';
import {ClassHelper} from '@enonic/lib-admin-ui/ClassHelper';

export class SetUserPasswordEvent
extends Event {

private readonly password: string;

constructor(password: string) {
super();
this.password = password;
}

getPassword(): string {
return this.password;
}

static on(handler: (event: SetUserPasswordEvent) => void, contextWindow: Window = window): void {
Event.bind(ClassHelper.getFullName(this), handler, contextWindow);
}

static un(handler?: (event: SetUserPasswordEvent) => void, contextWindow: Window = window): void {
Event.unbind(ClassHelper.getFullName(this), handler, contextWindow);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {OpenChangePasswordDialogEvent} from './OpenChangePasswordDialogEvent';
import {PasswordGenerator} from './PasswordGenerator';
import {Principal} from '@enonic/lib-admin-ui/security/Principal';
import {Validators} from '@enonic/lib-admin-ui/ui/form/Validators';
import {FormItem, FormItemBuilder} from '@enonic/lib-admin-ui/ui/form/FormItem';
import {Button} from '@enonic/lib-admin-ui/ui/button/Button';
import {i18n} from '@enonic/lib-admin-ui/util/Messages';
Expand All @@ -10,18 +8,17 @@ import {UserItemWizardStepForm} from './UserItemWizardStepForm';
import {NewPublicKeyDialog} from './NewPublicKeyDialog';
import {User} from '../principal/User';
import {PublicKeysGrid} from '../view/PublicKeysGrid';
import {SetUserPasswordEvent} from './SetUserPasswordEvent';

export class UserPasswordWizardStepForm
extends UserItemWizardStepForm {

private password: PasswordGenerator;
private password: string;

private changePasswordButton: Button;

private addPublicKeyButton: Button;

private createPasswordFormItem: FormItem;

private updatePasswordFormItem: FormItem;

private addPublicKeyFormItem: FormItem;
Expand All @@ -30,42 +27,38 @@ export class UserPasswordWizardStepForm

private publicKeysGrid: PublicKeysGrid;

constructor() {
constructor(principal?: Principal) {
super('user-password-wizard-step-form');

this.principal = principal;
}

protected initElements(): void {
super.initElements();

this.password = new PasswordGenerator();
this.changePasswordButton = new Button(i18n('action.changePassword'));
this.changePasswordButton = new Button(i18n(!this.principal ? 'action.setPassword' : 'action.changePassword'));
this.addPublicKeyButton = new Button(i18n('action.add'));
this.publicKeysGrid = new PublicKeysGrid();
}

protected postInitElements(): void {
super.postInitElements();

this.updatePasswordFormItem.setVisible(false);
this.addPublicKeyFormItem.setVisible(false);
}

protected createFormItems(): FormItem[] {
this.createPasswordFormItem = new FormItemBuilder(this.password)
.setLabel(i18n('field.password')).setValidator(Validators.required).build();

this.updatePasswordFormItem = new FormItemBuilder(this.changePasswordButton).build();

this.addPublicKeyFormItem = new FormItemBuilder(this.addPublicKeyButton).setLabel(i18n('field.userKeys.grid.title')).build();

return [this.createPasswordFormItem, this.updatePasswordFormItem, this.addPublicKeyFormItem];
return [this.updatePasswordFormItem, this.addPublicKeyFormItem];
}

protected initListeners(): void {
super.initListeners();

this.form.onValidityChanged((event: ValidityChangedEvent) => {
this.createPasswordFormItem.toggleClass(FormItem.INVALID_CLASS, !event.isValid());
this.updatePasswordFormItem.toggleClass(FormItem.INVALID_CLASS, !event.isValid());
this.addPublicKeyFormItem.toggleClass(FormItem.INVALID_CLASS, !event.isValid());
});
Expand All @@ -79,6 +72,10 @@ export class UserPasswordWizardStepForm
const publicKeysDialog = new NewPublicKeyDialog(user);
publicKeysDialog.open();
});

SetUserPasswordEvent.on((event) => {
this.password = event.getPassword();
});
}

layout(principal: Principal): void {
Expand All @@ -88,26 +85,21 @@ export class UserPasswordWizardStepForm
updatePrincipal(principal: Principal): void {
this.principal = principal;

if (principal) {
this.fieldSet.removeItem(this.createPasswordFormItem);
this.updatePasswordFormItem.setVisible(true);
if (principal.getKey().getIdProvider().isSystem()) {
this.addPublicKeyFormItem.setVisible(true);
this.publicKeysGrid.setUser(principal as User);
}
if (principal && principal.getKey().getIdProvider().isSystem()) {
this.addPublicKeyFormItem.setVisible(true);
this.publicKeysGrid.setUser(principal as User);
}
}

isValid(): boolean {
return !!this.principal || this.password.isValid();
}
// isValid(): boolean {
// if (!!this.principal && !this.password) {
// return new PasswordGenerator().setValue(this.password).isValid();
// }
// return true;
// }

getPassword(): string {
return this.password.getValue();
}

giveFocus(): boolean {
return this.password.giveFocus();
return this.password;
}

doRender(): Q.Promise<boolean> {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/js/app/wizard/UserWizardPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class UserWizardPanel
const steps: WizardStep[] = [];

this.userEmailWizardStepForm = new UserEmailWizardStepForm(this.getParams().idProvider.getKey(), this.isSystemUserItem());
this.userPasswordWizardStepForm = new UserPasswordWizardStepForm();
this.userPasswordWizardStepForm = new UserPasswordWizardStepForm(principal);
this.userMembershipsWizardStepForm = new UserMembershipsWizardStepForm();

if (!this.isSystemUserItem()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class CreateUserRequest

/* eslint-disable max-len */
getMutation(): string {
return `mutation ($key: String!, $displayName: String!, $email: String!, $login: String!, $password: String!, $memberships: [String]) {
return `mutation ($key: String!, $displayName: String!, $email: String!, $login: String!, $password: String, $memberships: [String]) {
createUser(key: $key, displayName: $displayName, email: $email, login: $login, password: $password, memberships: $memberships) {
key
login
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/i18n/phrases.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ app.abbr=UM
#
action.sync = Sync
action.changePassword = Change Password
action.setPassword = Set Password
action.create = Create
action.new.more=New...
action.new.user=New User
Expand Down Expand Up @@ -82,6 +83,7 @@ notify.delete.idprovider.single=Id Provider "{0}" is deleted
notify.delete.principal.multiple = {0} users were deleted
notify.delete.idprovider.multiple={0} id providers were deleted
notify.change.password = Password was changed
notify.set.password = Password is set.
notify.create.user = User was created
notify.create.group = Group was created
notify.create.role = Role was created
Expand All @@ -104,6 +106,7 @@ dialog.principal.update = Received Principal from server differs from what you h

dialog.changePassword.title = Change password
dialog.changePassword.msg = Password will be updated immediately after finishing
dialog.setPassword.title = Set password

dialog.new=Create New

Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/lib/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ exports.create = function createUser(params) {
principals.addMemberships(key, mms);
}

var password = common.required(params, 'password');
exports.updatePwd(key, password);
if (params.password) {
exports.updatePwd(key, params.password);
}

populateMemberships(createdUser);

Expand Down

0 comments on commit 975a63e

Please sign in to comment.