Skip to content

Commit

Permalink
Add MasterAdmin
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh committed Nov 19, 2024
1 parent 723639f commit ee573f0
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 49 deletions.
6 changes: 3 additions & 3 deletions backend/src/controllers/IntegrationController.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Request, Response } from "express";

import { getIO } from "../libs/socket";
import AppError from "../errors/AppError";
import { getIO } from "../libs/socket";

import UpdateIntegrationService from "../services/IntegrationServices/UpdateIntegrationService";
import ListIntegrationsService from "../services/IntegrationServices/ListIntegrationsService";
import UpdateIntegrationService from "../services/IntegrationServices/UpdateIntegrationService";

export const index = async (req: Request, res: Response): Promise<Response> => {
if (req.user.profile === "") {
Expand All @@ -20,7 +20,7 @@ export const update = async (
req: Request,
res: Response
): Promise<Response> => {
if (req.user.profile !== "admin") {
if (req.user.profile !== "admin" && req.user.profile !== "masteradmin") {
throw new AppError("ERR_NO_PERMISSION", 403);
}
const { integrationKey: key } = req.params;
Expand Down
6 changes: 3 additions & 3 deletions backend/src/controllers/SettingController.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Request, Response } from "express";

import { getIO } from "../libs/socket";
import AppError from "../errors/AppError";
import { getIO } from "../libs/socket";

import UpdateSettingService from "../services/SettingServices/UpdateSettingService";
import ListSettingsService from "../services/SettingServices/ListSettingsService";
import UpdateSettingService from "../services/SettingServices/UpdateSettingService";

export const index = async (req: Request, res: Response): Promise<Response> => {
if (req.user.profile === "") {
Expand All @@ -20,7 +20,7 @@ export const update = async (
req: Request,
res: Response
): Promise<Response> => {
if (req.user.profile !== "admin") {
if (req.user.profile !== "admin" && req.user.profile !== "masteradmin") {
throw new AppError("ERR_NO_PERMISSION", 403);
}
const { settingKey: key } = req.params;
Expand Down
8 changes: 4 additions & 4 deletions backend/src/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Request, Response } from "express";
import { getIO } from "../libs/socket";

import CheckSettingsHelper from "../helpers/CheckSettings";
import AppError from "../errors/AppError";
import CheckSettingsHelper from "../helpers/CheckSettings";

import CreateUserService from "../services/UserServices/CreateUserService";
import DeleteUserService from "../services/UserServices/DeleteUserService";
import ListUsersService from "../services/UserServices/ListUsersService";
import UpdateUserService from "../services/UserServices/UpdateUserService";
import ShowUserService from "../services/UserServices/ShowUserService";
import DeleteUserService from "../services/UserServices/DeleteUserService";
import UpdateUserService from "../services/UserServices/UpdateUserService";

type IndexQuery = {
searchParam: string;
Expand Down Expand Up @@ -119,7 +119,7 @@ export const remove = async (
): Promise<Response> => {
const { userId } = req.params;

if (req.user.profile !== "admin") {
if (req.user.profile !== "admin" && req.user.profile !== "masteradmin") {
throw new AppError("ERR_NO_PERMISSION", 403);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { QueryInterface } from "sequelize";

module.exports = {
up: async (queryInterface: QueryInterface) => {
return queryInterface.bulkInsert(
"Users",
[
{
name: "MasterAdmin",
email: "[email protected]",
passwordHash:
"$2a$08$nLlBSlHj.6XJNFLq.FSjVOjp4rSFHtFYHSUewBIQhceOv4gXU3yLC",
profile: "masteradmin",
tokenVersion: 0,
createdAt: new Date(),
updatedAt: new Date()
}
],
{}
);
},

down: async (queryInterface: QueryInterface) => {
return queryInterface.bulkDelete("Users", {});
}
};
21 changes: 13 additions & 8 deletions backend/src/services/UserServices/ListUsersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ const ListUsersService = async ({
pageNumber = "1"
}: Request): Promise<Response> => {
const whereCondition = {
[Op.or]: [
[Op.and]: [
{ profile: { [Op.ne]: "masteradmin" } },
{
"$User.name$": Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("User.name")),
"LIKE",
`%${searchParam.toLowerCase()}%`
)
},
{ email: { [Op.like]: `%${searchParam.toLowerCase()}%` } }
[Op.or]: [
{
"$User.name$": Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("User.name")),
"LIKE",
`%${searchParam.toLowerCase()}%`
)
},
{ email: { [Op.like]: `%${searchParam.toLowerCase()}%` } }
]
}
]
};
const limit = 20;
Expand Down
24 changes: 19 additions & 5 deletions docs/INSTALL_VPS.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,14 @@ npm install
#URL BACKEND
REACT_APP_BACKEND_URL=https://back.pressticket.com.br

#Tempo de encerramento automático dos tickets
#Tempo de encerramento automático dos tickets em horas
REACT_APP_HOURS_CLOSE_TICKETS_AUTO=

#Nome da Guia do navegador
REACT_APP_PAGE_TITLE=PressTicket

#PORTA do frontend
PORT=3333

#Para permitir acesso apenas do MasterAdmin (sempre ON)
REACT_APP_MASTERADMIN=OFF
```

### 8.4 Editando o arquivo .env do frontend usando os dados do item 8.3
Expand Down Expand Up @@ -464,7 +464,7 @@ sudo nano /etc/nginx/nginx.conf
### 9.12 Incluir no arquivos de configuração do nginx dentro do http no item 9.11

```
client_max_body_size 20M;
client_max_body_size 50M;
```

### 9.13 Testando o Nginx
Expand Down Expand Up @@ -522,3 +522,17 @@ Senha:
```
admin
```

# Seção 12: Usuário Master para Acesso

Usuário:

```
[email protected]
```

Senha:

```
masteradmin
```
1 change: 0 additions & 1 deletion docs/INSTALL_localhost.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ Crie ou edite o arquivo `.env` no diretório `frontend` com as seguintes informa
```bash
REACT_APP_BACKEND_URL=http://localhost:8080
REACT_APP_HOURS_CLOSE_TICKETS_AUTO=
REACT_APP_PAGE_TITLE=PressTicket
PORT=3333
```

Expand Down
8 changes: 4 additions & 4 deletions frontend/public/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="pt-br">
<head>
<title>%REACT_APP_PAGE_TITLE%</title>
<title>Carregando...</title>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
Expand All @@ -16,9 +16,9 @@
<noscript id="env-insertion-point"></noscript>
</head>
<body>
<noscript
>Você precisa habilitar o JavaScript para rodar este app.</noscript
>
<noscript>
Você precisa habilitar o JavaScript para rodar este app.
</noscript>
<div id="root"></div>
</body>
</html>
36 changes: 36 additions & 0 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "react-toastify/dist/ReactToastify.css";
import toastError from "./errors/toastError";
import Routes from "./routes";
import api from "./services/api";
import openSocket from "./services/socket-io";
import loadThemeConfig from './themes/themeConfig';

const App = () => {
Expand Down Expand Up @@ -88,6 +89,41 @@ const App = () => {
document.head.appendChild(favicon);
}, [theme]);

useEffect(() => {
const fetchPageTitle = async () => {
try {
const { data } = await api.get("/personalizations");

if (data && data.length > 0) {

const lightConfig = data.find(themeConfig => themeConfig.theme === "light");

if (lightConfig) {
document.title = lightConfig.company;
} else {
document.title = "Press Ticket";
}
}
} catch (err) {
toastError(err);
document.title = "Erro ao carregar título";
}
};

const socket = openSocket();
socket.on("personalization", data => {
if (data.action === "update") {
fetchPageTitle();
}
});

fetchPageTitle();

return () => {
socket.disconnect();
};
}, []);

const selectedTheme = loadThemeConfig(theme, theme === "light" ? lightThemeConfig : darkThemeConfig, locale);

return (
Expand Down
42 changes: 21 additions & 21 deletions frontend/src/pages/Settings/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import React, { useEffect, useState } from "react";
import { useHistory } from "react-router-dom";
import openSocket from "socket.io-client";

import { Box, Container, makeStyles, Tab, Tabs } from "@material-ui/core";
import React, { useContext, useEffect, useState } from "react";
import { useHistory } from "react-router-dom";
import { toast } from "react-toastify";

import ErrorBoundary from "../../components/ErrorBoundary";
import { AuthContext } from "../../context/Auth/AuthContext";
import toastError from "../../errors/toastError";
import api from "../../services/api";
import openSocket from "../../services/socket-io.js";
import { i18n } from "../../translate/i18n.js";

import ErrorBoundary from "../../components/ErrorBoundary";
import ComponentSettings from "./ComponentSettings";
import Personalize from "./Personalize.js";

Expand All @@ -19,7 +17,7 @@ const useStyles = makeStyles(theme => ({
display: "flex",
height: "92%",
backgroundColor: theme.palette.background.paper,
padding: theme.spacing(1)
padding: theme.spacing(1),
},
tabs: {
borderRight: `1px solid ${theme.palette.divider}`,
Expand All @@ -35,6 +33,8 @@ const Settings = ({ onThemeConfigUpdate }) => {
const history = useHistory();
const [settings, setSettings] = useState([]);
const [tabValue, setTabValue] = useState(0);
const { user } = useContext(AuthContext);
const isMasterAdminEnabled = process.env.REACT_APP_MASTERADMIN === "ON";

useEffect(() => {
let isMounted = true;
Expand All @@ -59,13 +59,13 @@ const Settings = ({ onThemeConfigUpdate }) => {

useEffect(() => {
let isMounted = true;
const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
const socket = openSocket();

socket.on("settings", data => {
socket.on("settings", (data) => {
if (isMounted && data.action === "update") {
setSettings(prevState => {
setSettings((prevState) => {
const aux = [...prevState];
const settingIndex = aux.findIndex(s => s.key === data.setting.key);
const settingIndex = aux.findIndex((s) => s.key === data.setting.key);
if (settingIndex !== -1) {
aux[settingIndex].value = data.setting.value;
}
Expand All @@ -80,7 +80,7 @@ const Settings = ({ onThemeConfigUpdate }) => {
};
}, []);

const handleChangeBooleanSetting = async e => {
const handleChangeBooleanSetting = async (e) => {
const selectedValue = e.target.checked ? "enabled" : "disabled";
const settingKey = e.target.name;

Expand All @@ -95,7 +95,7 @@ const Settings = ({ onThemeConfigUpdate }) => {
}
};

const handleChangeSetting = async e => {
const handleChangeSetting = async (e) => {
const selectedValue = e.target.value;
const settingKey = e.target.name;

Expand All @@ -109,8 +109,8 @@ const Settings = ({ onThemeConfigUpdate }) => {
}
};

const getSettingValue = key => {
const setting = settings.find(s => s.key === key);
const getSettingValue = (key) => {
const setting = settings.find((s) => s.key === key);
return setting ? setting.value : "";
};

Expand All @@ -128,7 +128,9 @@ const Settings = ({ onThemeConfigUpdate }) => {
className={classes.tabs}
>
<Tab label={i18n.t("settings.title")} />
<Tab label="Personalização" />
{(!isMasterAdminEnabled || user.profile === "masteradmin") && (
<Tab label="Personalização" />
)}
</Tabs>
<Box p={3}>
{tabValue === 0 && (
Expand All @@ -143,12 +145,10 @@ const Settings = ({ onThemeConfigUpdate }) => {
</ErrorBoundary>
</Container>
)}
{tabValue === 1 && (
{tabValue === 1 && (!isMasterAdminEnabled || user.profile === "masteradmin") && (
<Container className={classes.container}>
<ErrorBoundary>
<Personalize
onThemeConfigUpdate={onThemeConfigUpdate}
/>
<Personalize onThemeConfigUpdate={onThemeConfigUpdate} />
</ErrorBoundary>
</Container>
)}
Expand Down
13 changes: 13 additions & 0 deletions frontend/src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ const rules = {
"contacts-page:deleteContact",
],
},
masteradmin: {
static: [
"drawer-admin-items:view",
"tickets-manager:showall",
"user-modal:editProfile",
"user-modal:editQueues",
"user-table:editTricked",
"ticket-options:deleteTicket",
"ticket-options:transferWhatsapp",
"contacts-page:deleteContact",
"settings:personalize",
],
}
};

export default rules;

0 comments on commit ee573f0

Please sign in to comment.