Skip to content

Commit

Permalink
feat: apply profile rules (#163)
Browse files Browse the repository at this point in the history
* feat: apply profile rules
  • Loading branch information
danilolutz authored Mar 18, 2022
1 parent f211b33 commit 4f7f572
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
28 changes: 22 additions & 6 deletions src/app/components/UserTypeSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React, {
useState,
} from 'react';
import { OnChangeValue } from 'react-select';

import { useAuth } from '../../hooks/auth';
import { Container } from './style';
import i18n from '../../i18n';
import SelectInput, { SelectHandles } from '../SelectInput';
Expand All @@ -30,6 +30,8 @@ const UserTypeSelect: React.ForwardRefRenderFunction<
SelectHandles,
UserTypeProps
> = ({ containerStyle, autoFocus, handleCustomChange }, selectRef) => {
const { user } = useAuth();

const [options, setOptions] = useState<Option[]>([]);
const [value, setValue] = useState(undefined);

Expand All @@ -38,13 +40,27 @@ const UserTypeSelect: React.ForwardRefRenderFunction<
entity: 'UserType',
}) as UserType[];

const mappedOptions = result.map((item) => ({
label: i18n.t(`userType.${item.name}`),
value: item.id.toString(),
}));
const filtered = result.filter((item) => {
if (user.userType.name === 'librarian') {
return item.name !== 'admin';
}

if (user.userType.name === 'person') {
return item.name === 'person';
}

return item;
});

const mappedOptions = filtered.map((item) => {
return {
label: i18n.t(`userType.${item.name}`),
value: item.id.toString(),
};
});

setOptions(mappedOptions);
}, []);
}, [user.userType.name]);

const handleChange = (selectedValue: OnChangeValue<Option, false>) => {
setValue(selectedValue);
Expand Down
8 changes: 4 additions & 4 deletions src/app/hooks/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ interface User {
name: string;
login: string;
password: string;
userType: {
name: string;
};
}

interface AuthState {
Expand Down Expand Up @@ -52,15 +55,12 @@ const AuthProvider: React.FC = ({ children }) => {
const user = window.api.sendSync('userLogin', {
entity: 'User',
value: {
where: {
login,
},
login,
password,
},
}) as User;

if (user) {

localStorage.setItem('@librarian:user', JSON.stringify(user));
setData({ user });
return true;
Expand Down
20 changes: 16 additions & 4 deletions src/electron/infra/db/repositories/user/UserLoginRepository.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import typeORM from 'typeorm';
import typeORM, { In } from 'typeorm';
import bcrypt from 'bcryptjs';
import { User } from '../../../../database/models/User.schema';
import RepositoryBase from '../../RepositoryBase';

interface Where {
where: unknown;
login: string;
password: string;
}

Expand All @@ -17,7 +17,9 @@ export class UserLoginRepository extends RepositoryBase {
super();
}

public static getInstance(typeOrm: typeORM.Repository<unknown>): UserLoginRepository {
public static getInstance(
typeOrm: typeORM.Repository<unknown>
): UserLoginRepository {
if (!UserLoginRepository.instance) {
UserLoginRepository.instance = new UserLoginRepository();
}
Expand All @@ -29,7 +31,17 @@ export class UserLoginRepository extends RepositoryBase {

public async execute(content: Where): Promise<unknown> {
try {
const user = (await this.repository.findOne(content.where)) as User;
const filter = {
relations: ['userType'],
where: {
login: content.login,
userType: {
name: In(['admin', 'librarian']),
},
},
};

const user = (await this.repository.findOne(filter)) as User;

if (user && bcrypt.compareSync(content.password, user.password)) {
return user;
Expand Down

0 comments on commit 4f7f572

Please sign in to comment.