1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-18 07:52:23 +01:00

(ui) allow admins to change user password without asking for the current

This commit is contained in:
Sylvain 2022-05-10 10:50:41 +02:00
parent 302c55755e
commit 89853d3533
5 changed files with 37 additions and 2 deletions

View File

@ -234,6 +234,12 @@ class API::MembersController < API::ApiController
render json: @member
end
def current
@member = current_user
authorize @member
render json: @member
end
private
def set_member

View File

@ -34,4 +34,9 @@ export default class MemberAPI {
});
return res?.data;
}
static async current (): Promise<User> {
const res: AxiosResponse<User> = await apiClient.get('/api/members/current');
return res?.data;
}
}

View File

@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect } from 'react';
import { FabButton } from '../base/fab-button';
import { FabModal } from '../base/fab-modal';
import { FormInput } from '../form/form-input';
@ -8,6 +8,7 @@ import Authentication from '../../api/authentication';
import { FieldValues } from 'react-hook-form/dist/types/fields';
import { PasswordInput } from './password-input';
import { FormState } from 'react-hook-form/dist/types/form';
import MemberAPI from '../../api/member';
interface ChangePasswordProp<TFieldValues> {
register: UseFormRegister<TFieldValues>,
@ -25,9 +26,16 @@ export const ChangePassword = <TFieldValues extends FieldValues>({ register, onE
const [isModalOpen, setIsModalOpen] = React.useState<boolean>(false);
const [isConfirmedPassword, setIsConfirmedPassword] = React.useState<boolean>(false);
const [isPrivileged, setIsPrivileged] = React.useState<boolean>(false);
const { handleSubmit, register: passwordRegister } = useForm<{ password: string }>();
useEffect(() => {
MemberAPI.current().then(user => {
setIsPrivileged(user.role === 'admin' || user.role === 'manager');
}).catch(error => onError(error));
}, []);
/**
* Opens/closes the dialog asking to confirm the current password before changing it.
*/
@ -35,6 +43,17 @@ export const ChangePassword = <TFieldValues extends FieldValues>({ register, onE
setIsModalOpen(!isModalOpen);
};
/**
* Callback triggered when the user clicks on the "change my password" button
*/
const handleChangePasswordRequested = () => {
if (isPrivileged) {
setIsConfirmedPassword(true);
} else {
toggleConfirmationModal();
}
};
/**
* Callback triggered when the user confirms his current password.
*/
@ -59,7 +78,7 @@ export const ChangePassword = <TFieldValues extends FieldValues>({ register, onE
return (
<div className="change-password">
{!isConfirmedPassword && <FabButton onClick={() => toggleConfirmationModal()}>
{!isConfirmedPassword && <FabButton onClick={() => handleChangePasswordRequested()}>
{t('app.shared.change_password.change_my_password')}
</FabButton>}
{isConfirmedPassword && <div className="password-fields">

View File

@ -19,6 +19,10 @@ class UserPolicy < ApplicationPolicy
user.admin? || user.manager? || (record.is_allow_contact && record.member?) || (user.id == record.id)
end
def current?
user.admin? || user.manager? || (user.id == record.id)
end
def update?
user.admin? || user.manager? || (user.id == record.id)
end

View File

@ -55,6 +55,7 @@ Rails.application.routes.draw do
get '/export_subscriptions', action: 'export_subscriptions', on: :collection
get '/export_reservations', action: 'export_reservations', on: :collection
get '/export_members', action: 'export_members', on: :collection
get 'current', action: 'current', on: :collection
put ':id/merge', action: 'merge', on: :collection
post 'list', action: 'list', on: :collection
get 'search/:query', action: 'search', on: :collection