Skip to content

Commit

Permalink
feat:Take user role from cookie #335
Browse files Browse the repository at this point in the history
  • Loading branch information
jacovinus committed May 17, 2023
1 parent dfefc47 commit 66f0b62
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/plugins/Clock/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const clockPlugin: Plugin = {
Component: Clock,
description: "A simple clock with date / time",
active: false,
roles:['admin','user','guest']
roles:['admin','user','guest','superAdmin']
};

export default clockPlugin;
4 changes: 2 additions & 2 deletions src/plugins/Plugins.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const PluginCards: React.FC<{
section: string;
}> = ({ components, section }) => {

const userType = useSelector((store:any)=>store.userType)
const userType = useSelector((store:any)=>store.currentUser.role)

const filteredList = useMemo(()=>{
return components?.filter((f:any)=> f.roles.includes(userType))
Expand All @@ -181,7 +181,7 @@ export const PluginCards: React.FC<{

export default function Plugins() {
const theme = useTheme();
const userType = useSelector((store:any)=>store.userType)
const userType = useSelector((store:any)=>store.currentUser.role)

console.log(userType)

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/settingsmenu/MainMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Avatar from "react-avatar";
export default function MainMenu() {
const showDs = useSelector((store: any) => store.showDataSourceSetting);
const currentUser = useSelector((store:any) => store.currentUser);
const userType = useSelector((store: any) => store.userType);
const userType = useSelector((store: any) => store.currentUser.role);
const dispatch = useDispatch();

const theme = useTheme();
Expand Down
2 changes: 1 addition & 1 deletion src/store/createInitialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default function initialState() {
isSubmit: urlState.isSubmit || false,
isEmbed: urlState.isEmbed || false,
userType:'admin',
currentUser: {name:'Qryn Admin', role:'admin', id:"OoH8zZt71U70W01LcgAQs"},
currentUser: {name:'Qryn Admin', role:'admin', id:"OoH8zZt71U70W01LcgAQs", selected:true},
// dont mention queries // its obvious

left: urlState["left"] || [
Expand Down
5 changes: 4 additions & 1 deletion src/views/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export default function Main() {

UpdateStateFromQueryParams();

const { cookiesAvailable, cookieAuth } = useCookiesAvailable(paramsMemo);
const { cookiesAvailable, cookieAuth, cookieUser } = useCookiesAvailable(paramsMemo);



const { urlAvailable, url } = useUrlAvailable(paramsMemo);
useEffect(() => {
const onlyCookie = cookiesAvailable && !urlAvailable;
Expand Down
19 changes: 0 additions & 19 deletions src/views/Main/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import axios, { AxiosError, AxiosResponse } from "axios";
import { getDsHeaders } from "../../components/QueryBuilder/Operations/helpers";
import setDataSources from "../DataSources/store/setDataSources";
import { setUserType } from "./actions";
import { setShowDataSourceSetting } from "./setShowDataSourceSetting";

// updateDataSources:
Expand All @@ -28,24 +27,6 @@ export function updateDataSourcesWithUrl(
let cookieDsData = "";
if (dsData && dsData !== "") {
try {
let userType = "";
let ut = dsData.split("&&");
if (ut[1] && typeof ut[1] === "string" && ut[1] !== "") {
userType = ut[1];
} else {
userType = "admin";
}

// urlDsData = atob(urlDsData);
// urlDsData = JSON.parse(urlDsData);

if (
userType &&
typeof userType === "string" &&
userType !== ""
) {
dispatch(setUserType(userType));
}
if (typeof cookieDsData === "object" && cookieDsData["url"]) {
apiUrl = cookieDsData["url"];
haveUrl = true;
Expand Down
29 changes: 28 additions & 1 deletion src/views/Main/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { useMemo } from "react";
import { useCookies } from "react-cookie";
import DOMPurify from 'isomorphic-dompurify';
import {useSelector, useDispatch} from 'react-redux';
import { setCurrentUser } from "../User/actions";


// useCookiesAvailable:



export function useCookiesAvailable(urlParams: any) {

const dispatch = useDispatch()
let cookieAuth = "";
let cookiesAvailable = false;

const currentUser = useSelector((store:any) => store.currentUser)

const hasCookie = useMemo(() => {
return urlParams.has("cookie") || false;
}, [urlParams]);
Expand All @@ -19,12 +29,29 @@ export function useCookiesAvailable(urlParams: any) {
}, [urlParams, hasCookie]);
// eslint-disable-next-line
const [cookie, _] = useCookies([cookieParam]);
// eslint-disable-next-line
const [userCookie, __] = useCookies(['user-cookie'])



let userCookieParsed = currentUser


if(userCookie?.['user-cookie']) {
try {
userCookieParsed = atob(userCookie['user-cookie'])

}catch(e){console.log("could not parse user cookie")}

}



if (cookie[cookieParam] && cookie[cookieParam] !== "") {
cookieAuth = cookie[cookieParam];
cookiesAvailable = true;
}
return { cookieAuth, cookiesAvailable };
return { cookieAuth, cookiesAvailable, cookieUser: userCookieParsed };
}

// useUrlAvailable:
Expand Down
Loading

0 comments on commit 66f0b62

Please sign in to comment.