forked from gitroomhq/postiz-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
343 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,71 @@ | ||
import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import {AuthService} from "@gitroom/helpers/auth/auth.service"; | ||
import {User} from '@prisma/client'; | ||
import {OrganizationService} from "@gitroom/nestjs-libraries/database/prisma/organizations/organization.service"; | ||
import { AuthService } from '@gitroom/helpers/auth/auth.service'; | ||
import { User } from '@prisma/client'; | ||
import { OrganizationService } from '@gitroom/nestjs-libraries/database/prisma/organizations/organization.service'; | ||
import { UsersService } from '@gitroom/nestjs-libraries/database/prisma/users/users.service'; | ||
|
||
@Injectable() | ||
export class AuthMiddleware implements NestMiddleware { | ||
constructor( | ||
private _organizationService: OrganizationService, | ||
) { | ||
constructor( | ||
private _organizationService: OrganizationService, | ||
private _userService: UsersService | ||
) {} | ||
async use(req: Request, res: Response, next: NextFunction) { | ||
const auth = req.headers.auth || req.cookies.auth; | ||
if (!auth) { | ||
throw new Error('Unauthorized'); | ||
} | ||
async use(req: Request, res: Response, next: NextFunction) { | ||
const auth = req.headers.auth || req.cookies.auth; | ||
if (!auth) { | ||
throw new Error('Unauthorized'); | ||
} | ||
try { | ||
const user = AuthService.verifyJWT(auth) as User | null; | ||
const orgHeader = req.cookies.showorg || req.headers.showorg; | ||
try { | ||
let user = AuthService.verifyJWT(auth) as User | null; | ||
const orgHeader = req.cookies.showorg || req.headers.showorg; | ||
|
||
if (!user) { | ||
throw new Error('Unauthorized'); | ||
} | ||
if (!user) { | ||
throw new Error('Unauthorized'); | ||
} | ||
|
||
delete user.password; | ||
const organization = (await this._organizationService.getOrgsByUserId(user.id)).filter(f => !f.users[0].disabled); | ||
const setOrg = organization.find((org) => org.id === orgHeader) || organization[0]; | ||
if (user?.isSuperAdmin && req.cookies.impersonate) { | ||
const loadImpersonate = await this._organizationService.getUserOrg( | ||
req.cookies.impersonate | ||
); | ||
|
||
if (loadImpersonate) { | ||
user = loadImpersonate.user; | ||
user.isSuperAdmin = true; | ||
delete user.password; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
req.user = user; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
req.user = user; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
req.org = setOrg; | ||
} | ||
catch (err) { | ||
throw new Error('Unauthorized'); | ||
// @ts-ignore | ||
loadImpersonate.organization.users = loadImpersonate.organization.users.filter(f => f.userId === user.id); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
req.org = loadImpersonate.organization; | ||
next(); | ||
return ; | ||
} | ||
console.log('Request...'); | ||
next(); | ||
} | ||
|
||
delete user.password; | ||
const organization = ( | ||
await this._organizationService.getOrgsByUserId(user.id) | ||
).filter((f) => !f.users[0].disabled); | ||
const setOrg = | ||
organization.find((org) => org.id === orgHeader) || organization[0]; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
req.user = user; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
req.org = setOrg; | ||
} catch (err) { | ||
throw new Error('Unauthorized'); | ||
} | ||
console.log('Request...'); | ||
next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { Input } from '@gitroom/react/form/input'; | ||
import { useCallback, useMemo, useState } from 'react'; | ||
import useSWR from 'swr'; | ||
import { useFetch } from '@gitroom/helpers/utils/custom.fetch'; | ||
import { useUser } from '@gitroom/frontend/components/layout/user.context'; | ||
|
||
export const Impersonate = () => { | ||
const fetch = useFetch(); | ||
const [name, setName] = useState(''); | ||
const user = useUser(); | ||
|
||
const load = useCallback(async () => { | ||
if (!name) { | ||
return []; | ||
} | ||
|
||
const value = await (await fetch(`/user/impersonate?name=${name}`)).json(); | ||
return value; | ||
}, [name]); | ||
|
||
const stopImpersonating = useCallback(async () => { | ||
await fetch(`/user/impersonate`, { | ||
method: 'POST', | ||
body: JSON.stringify({ id: '' }), | ||
}); | ||
|
||
window.location.reload(); | ||
}, []); | ||
|
||
const setUser = useCallback( | ||
(userId: string) => async () => { | ||
await fetch(`/user/impersonate`, { | ||
method: 'POST', | ||
body: JSON.stringify({ id: userId }), | ||
}); | ||
|
||
window.location.reload(); | ||
}, | ||
[] | ||
); | ||
|
||
const { data } = useSWR(`/impersonate-${name}`, load, { | ||
refreshWhenHidden: false, | ||
revalidateOnMount: true, | ||
revalidateOnReconnect: false, | ||
revalidateOnFocus: false, | ||
refreshWhenOffline: false, | ||
revalidateIfStale: false, | ||
refreshInterval: 0, | ||
}); | ||
|
||
const mapData = useMemo(() => { | ||
return data?.map( | ||
(curr: any) => ({ | ||
id: curr.id, | ||
name: curr.user.name, | ||
email: curr.user.email, | ||
}), | ||
[] | ||
); | ||
}, [data]); | ||
|
||
return ( | ||
<div className="px-[23px]"> | ||
<div className="bg-forth h-[52px] flex justify-center items-center border-input border rounded-[8px]"> | ||
<div className="relative flex flex-col w-[600px]"> | ||
<div className="relative z-[999]"> | ||
{user?.impersonate ? ( | ||
<div className="text-center flex justify-center items-center gap-[20px]"> | ||
<div>Currently Impersonating</div> | ||
<div> | ||
<div | ||
className="px-[10px] rounded-[4px] bg-red-500 text-white cursor-pointer" | ||
onClick={stopImpersonating} | ||
> | ||
X | ||
</div> | ||
</div> | ||
</div> | ||
) : ( | ||
<Input | ||
autoComplete="off" | ||
placeholder="Write the user details" | ||
name="impersonate" | ||
disableForm={true} | ||
label="" | ||
removeError={true} | ||
value={name} | ||
onChange={(e) => setName(e.target.value)} | ||
/> | ||
)} | ||
</div> | ||
{!!data?.length && ( | ||
<> | ||
<div | ||
className="bg-black/80 fixed left-0 top-0 w-full h-full z-[998]" | ||
onClick={() => setName('')} | ||
/> | ||
<div className="absolute top-[100%] w-full left-0 bg-sixth border border-[#172034] text-white z-[999]"> | ||
{mapData?.map((user: any) => ( | ||
<div | ||
onClick={setUser(user.id)} | ||
key={user.id} | ||
className="p-[10px] border-b border-[#172034] hover:bg-tableBorder cursor-pointer" | ||
> | ||
user: {user.id.split('-').at(-1)} - {user.name} -{' '} | ||
{user.email} | ||
</div> | ||
))} | ||
</div> | ||
</> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.