Skip to content

Commit

Permalink
#70 update credential list user
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanwerfling committed Jun 30, 2024
1 parent 9fc26a9 commit 8d27ca5
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 3 deletions.
26 changes: 25 additions & 1 deletion frontend/src/inc/Api/Credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
CredentialResponse,
Credential as CredentialData,
SchemaCredentialProviderResponse,
SchemaCredentialResponse, SchemaDefaultReturn
SchemaCredentialResponse,
SchemaDefaultReturn,
CredentialUser,
CredentialUsersRequest,
SchemaCredentialUsersResponse
} from 'flyingfish_schemas';
import {NetFetch} from '../Net/NetFetch';

Expand Down Expand Up @@ -31,10 +35,30 @@ export class Credential {
/**
* Save a credential
* @param {CredentialData} data
* @returns {boolean}
*/
public static async save(data: CredentialData): Promise<boolean> {
await NetFetch.postData('/json/credential/save', data, SchemaDefaultReturn);
return true;
}

/**
* Return the credential userlist
* @param {number} credentialId
* @returns {CredentialUser[]}
*/
public static async getUserList(credentialId: number): Promise<CredentialUser[]> {
const req: CredentialUsersRequest = {
credential_id: credentialId
};

const response = await NetFetch.postData('/json/credential/user/list', req, SchemaCredentialUsersResponse);

if (response.list) {
return response.list;
}

return [];
}

}
192 changes: 192 additions & 0 deletions frontend/src/inc/Pages/Credential/CredentialUserEditModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import {
FormGroup,
InputBottemBorderOnly2,
InputType,
ModalDialog,
ModalDialogType,
Switch,
Element
} from 'bambooo';

/**
* CredentialUserEditModalButtonClickFn
*/
export type CredentialUserEditModalButtonClickFn = () => Promise<void>;

/**
* Credential user edit modal
*/
export class CredentialUserEditModal extends ModalDialog {

/**
* id of entry
* @protected
*/
protected _id: number|null = null;

/**
* input username
* @protected
*/
protected _inputUsername: InputBottemBorderOnly2;

/**
* input password
* @protected
*/
protected _inputPassword: InputBottemBorderOnly2;

/**
* input password repeat
* @protected
*/
protected _inputPasswordRepeat: InputBottemBorderOnly2;

/**
* switch disable
* @protected
*/
protected _switchDisable: Switch;

/**
* click save fn
* @protected
*/
protected _onSaveClick: CredentialUserEditModalButtonClickFn | null = null;

/**
* constructor
* @param elementObject
*/
public constructor(elementObject: Element) {
super(elementObject, 'credentialusermodaldialog', ModalDialogType.large);

const bodyCard = jQuery('<div class="card-body"/>').appendTo(this._body);

const groupUsername = new FormGroup(bodyCard, 'Username');
this._inputUsername = new InputBottemBorderOnly2(groupUsername);

const groupPassword = new FormGroup(bodyCard, 'Password');
this._inputPassword = new InputBottemBorderOnly2(groupPassword, undefined, InputType.password);

const groupPasswordRepeat = new FormGroup(bodyCard, 'Password repeat');
this._inputPasswordRepeat = new InputBottemBorderOnly2(groupPasswordRepeat, undefined, InputType.password);

const groupDisable = new FormGroup(bodyCard, 'Disable this User');
this._switchDisable = new Switch(groupDisable, 'userdisable');

// buttons -----------------------------------------------------------------------------------------------------

jQuery('<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>').appendTo(this._footer);
const btnSave = jQuery('<button type="button" class="btn btn-primary">Save changes</button>').appendTo(this._footer);

btnSave.on('click', async(): Promise<void> => {
if (this._onSaveClick !== null) {
this.showLoading();
await this._onSaveClick();
this.hideLoading();
}
});
}

/**
* getId
* @returns {number|null}
*/
public getId(): number|null {
return this._id;
}

/**
* setId
* @param {number} id
*/
public setId(id: number): void {
this._id = id;
}

/**
* Return the username
* @returns {string}
*/
public getUsername(): string {
return this._inputUsername.getValue();
}

/**
* Set the username
* @param {string} username
*/
public setUsername(username: string): void {
this._inputUsername.setValue(username);
}

/**
* Return the password
* @returns {string}
*/
public getPassword(): string {
return this._inputPassword.getValue();
}

/**
* Set the password
* @param {string} password
*/
public setPassword(password: string): void {
this._inputPassword.setValue(password);
}

/**
* Return the password repeat
* @returns {string}
*/
public getPasswordRepeat(): string {
return this._inputPasswordRepeat.getValue();
}

/**
* Set the password repeat
* @param {string} password
*/
public setPasswordRepeat(password: string): void {
this._inputPasswordRepeat.setValue(password);
}

/**
* Is the user disabled
* @returns {boolean}
*/
public isDisabled(): boolean {
return this._switchDisable.isEnable();
}

/**
* Set the user disabled
* @param {boolean} disable
*/
public setDisabled(disable: boolean): void {
this._switchDisable.setEnable(disable);
}

/**
* Set the on save function
* @param {CredentialUserEditModalButtonClickFn} onSave
*/
public setOnSave(onSave: CredentialUserEditModalButtonClickFn): void {
this._onSaveClick = onSave;
}

/**
* Reset all input fields
*/
public override resetValues(): void {
super.resetValues();
this._id = null;
this.setUsername('');
this.setPassword('');
this.setPasswordRepeat('');
this.setDisabled(false);
}

}
85 changes: 83 additions & 2 deletions frontend/src/inc/Pages/Credential/CredentialUsers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import {Card, ContentCol, ContentColSize, ContentRow, Table, Th, Tr} from 'bambooo';
import {
ButtonMenu,
ButtonType,
Card,
ContentCol,
ContentColSize,
ContentRow,
IconFa,
LeftNavbarLink,
Table,
Td,
Th,
Tr
} from 'bambooo';
import {Credential} from 'flyingfish_schemas';
import {Credential as CredentialAPI} from '../../Api/Credential';
import {UnauthorizedError} from '../../Api/Error/UnauthorizedError';
import {UtilRedirect} from '../../Utils/UtilRedirect';
import {BasePage} from '../BasePage';
import {CredentialUserEditModal} from './CredentialUserEditModal';

/**
* Credential Users page
*/
export class CredentialUsers extends BasePage {

/**
Expand All @@ -16,6 +36,12 @@ export class CredentialUsers extends BasePage {
*/
protected _credential: Credential;

/**
* Credential user dialog
* @protected
*/
protected _userDialog: CredentialUserEditModal;

/**
* constructor
*/
Expand All @@ -25,6 +51,22 @@ export class CredentialUsers extends BasePage {
this._credential = credential;

this.setTitle(`Credential User-List: ${this._credential.name}`);

// crdential user modal ----------------------------------------------------------------------------------------

this._userDialog = new CredentialUserEditModal(
this._wrapper.getContentWrapper().getContent()
);

// -------------------------------------------------------------------------------------------------------------

// eslint-disable-next-line no-new
new LeftNavbarLink(this._wrapper.getNavbar().getLeftNavbar(), 'Add Credential User', async() => {
this._userDialog.resetValues();
this._userDialog.setTitle('Add Credential User');
this._userDialog.show();
return false;
}, 'btn btn-block btn-default btn-sm', IconFa.add);
}

/**
Expand All @@ -46,7 +88,7 @@ export class CredentialUsers extends BasePage {
new Th(trhead, 'Name');

// eslint-disable-next-line no-new
new Th(trhead, 'Last');
new Th(trhead, 'Disabled');

// eslint-disable-next-line no-new
new Th(trhead, '');
Expand All @@ -58,6 +100,45 @@ export class CredentialUsers extends BasePage {
card.showLoading();
table.getTbody().empty();

try {
const users = await CredentialAPI.getUserList(this._credential.id);

for (const user of users) {
const trbody = new Tr(table.getTbody());

// eslint-disable-next-line no-new
new Td(trbody, `#${user.id}`);

// eslint-disable-next-line no-new
new Td(trbody, `${user.username}`);

// eslint-disable-next-line no-new
new Td(trbody, '');

const tdRAction = new Td(trbody, '');
const btnRMenu = new ButtonMenu(
tdRAction.getElement(),
IconFa.bars,
true,
ButtonType.borderless
);

btnRMenu.addMenuItem(
'Edit',
async(): Promise<void> => {
this._userDialog.resetValues();
this._userDialog.setTitle('Edit Credential User');


},
IconFa.edit
);
}
} catch (error) {
if (error instanceof UnauthorizedError) {
UtilRedirect.toLogin();
}
}

card.hideLoading();
};
Expand Down

0 comments on commit 8d27ca5

Please sign in to comment.