Skip to content

Commit

Permalink
Added new privilege for changing passwords.
Browse files Browse the repository at this point in the history
  • Loading branch information
evert committed Nov 21, 2023
1 parent 3d7e3d0 commit 685f938
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Changelog
covers these new ones.
* It wasn't possible to see a full principal even if a user had
`a12n:principal:list` privilege.
* Added new privilege for changing passwords: `a12n:user:change-password`.


0.24.0 (2023-11-09)
Expand Down
17 changes: 17 additions & 0 deletions src/migrations/20231121134632_new_privileges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex('privileges')
.insert({privilege: 'a12n:user:change-password', description: 'Allow changing a users\' password.'});

}


export async function down(knex: Knex): Promise<void> {

await knex('privileges')
.delete()
.whereIn('privileges', ['a12n:user:change-password']);

}

2 changes: 1 addition & 1 deletion src/privilege/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export type InternalPrivilege =
| 'a12n:principals:update'
| 'a12n:one-time-token:generate'
| 'a12n:one-time-token:exchange'

| 'a12n:user:change-password';
12 changes: 10 additions & 2 deletions src/user/controller/password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ import Controller from '@curveball/controller';
import { Context } from '@curveball/core';
import { PrincipalService } from '../../principal/service';
import * as userService from '../service';
import { UnprocessableEntity } from '@curveball/http-errors';

class UserPasswordController extends Controller {

async put(ctx: Context) {

ctx.privileges.require('admin');

const userBody: any = ctx.request.body;
const principalService = new PrincipalService(ctx.privileges);
const user = await principalService.findByExternalId(ctx.params.id, 'user');

ctx.privileges.require('a12n:user:change-password', user.href);

if (!userBody.newPassword || typeof userBody.newPassword !== 'string') {
throw new UnprocessableEntity('The "newPassword" property is required.');
}
if (userBody.newPassword.length < 8) {
throw new UnprocessableEntity('Passwords must be at least 8 characters.');
}

const password = userBody.newPassword;

await userService.updatePassword(user, password);
Expand Down
1 change: 0 additions & 1 deletion src/user/controller/privileges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as privilegeService from '../../privilege/service';
import { PrivilegeMap } from '../../privilege/types';
import * as hal from '../formats/hal';
import { PrincipalService } from '../../principal/service';
// import * as groupService from '../../group/service';

type PolicyForm = {
policyBody: string;
Expand Down
2 changes: 1 addition & 1 deletion src/user/formats/hal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function item(user: User, privileges: PrivilegeMap, hasControl: boolean,
title: 'App Permissions',
};
}
if (currentUserPrivileges.has('admin')) {
if (currentUserPrivileges.has('a12n:user:change-password', user.href)) {
hal._links['password'] = {
href: `${user.href}/password`,
title: 'Change user\'s password',
Expand Down

0 comments on commit 685f938

Please sign in to comment.