mirror of
https://github.com/sasjs/server.git
synced 2025-12-11 19:44:35 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff1def6436 | ||
|
|
c275db184e | ||
|
|
e4239fbcc3 | ||
|
|
c6fd8fdd70 | ||
|
|
79dc2dba23 | ||
|
|
2a7223ad7d |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,6 +4,7 @@ node_modules/
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
.env*
|
.env*
|
||||||
sas/
|
sas/
|
||||||
|
sasjs_root/
|
||||||
tmp/
|
tmp/
|
||||||
build/
|
build/
|
||||||
sasjsbuild/
|
sasjsbuild/
|
||||||
|
|||||||
19
CHANGELOG.md
19
CHANGELOG.md
@@ -1,3 +1,22 @@
|
|||||||
|
# [0.3.0](https://github.com/sasjs/server/compare/v0.2.0...v0.3.0) (2022-05-25)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **web:** added profile + edit + autoexec changes ([c275db1](https://github.com/sasjs/server/commit/c275db184e874f0ee3a4f08f2592cfacf1e90742))
|
||||||
|
|
||||||
|
# [0.2.0](https://github.com/sasjs/server/compare/v0.1.0...v0.2.0) (2022-05-25)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **autoexec:** usage in case of desktop from file ([79dc2db](https://github.com/sasjs/server/commit/79dc2dba23dc48ec218a973119392a45cb3856b5))
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **api:** added autoexec + major type setting changes ([2a7223a](https://github.com/sasjs/server/commit/2a7223ad7d6b8f3d4682447fd25d9426a7c79ac3))
|
||||||
|
|
||||||
# [0.1.0](https://github.com/sasjs/server/compare/v0.0.77...v0.1.0) (2022-05-23)
|
# [0.1.0](https://github.com/sasjs/server/compare/v0.0.77...v0.1.0) (2022-05-23)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -323,6 +323,8 @@ components:
|
|||||||
type: boolean
|
type: boolean
|
||||||
isAdmin:
|
isAdmin:
|
||||||
type: boolean
|
type: boolean
|
||||||
|
autoExec:
|
||||||
|
type: string
|
||||||
required:
|
required:
|
||||||
- id
|
- id
|
||||||
- displayName
|
- displayName
|
||||||
@@ -352,6 +354,10 @@ components:
|
|||||||
type: boolean
|
type: boolean
|
||||||
description: 'Account should be active or not, defaults to true'
|
description: 'Account should be active or not, defaults to true'
|
||||||
example: 'true'
|
example: 'true'
|
||||||
|
autoExec:
|
||||||
|
type: string
|
||||||
|
description: 'User-specific auto-exec code'
|
||||||
|
example: ""
|
||||||
required:
|
required:
|
||||||
- displayName
|
- displayName
|
||||||
- username
|
- username
|
||||||
@@ -537,7 +543,7 @@ paths:
|
|||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
properties:
|
properties:
|
||||||
user: {properties: {displayName: {type: string}, username: {type: string}}, required: [displayName, username], type: object}
|
user: {properties: {displayName: {type: string}, username: {type: string}, id: {type: number, format: double}}, required: [displayName, username, id], type: object}
|
||||||
loggedIn: {type: boolean}
|
loggedIn: {type: boolean}
|
||||||
required:
|
required:
|
||||||
- user
|
- user
|
||||||
@@ -989,6 +995,7 @@ paths:
|
|||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/components/schemas/UserDetailsResponse'
|
$ref: '#/components/schemas/UserDetailsResponse'
|
||||||
|
description: 'Only Admin or user itself will get user autoExec code.'
|
||||||
summary: 'Get user properties - such as group memberships, userName, displayName.'
|
summary: 'Get user properties - such as group memberships, userName, displayName.'
|
||||||
tags:
|
tags:
|
||||||
- User
|
- User
|
||||||
|
|||||||
@@ -30,9 +30,7 @@ dotenv.config()
|
|||||||
|
|
||||||
instantiateLogger()
|
instantiateLogger()
|
||||||
|
|
||||||
if (verifyEnvVariables()) {
|
if (verifyEnvVariables()) process.exit(ReturnCode.InvalidEnv)
|
||||||
process.exit(ReturnCode.InvalidEnv)
|
|
||||||
}
|
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
@@ -85,7 +83,7 @@ app.use(
|
|||||||
/***********************************
|
/***********************************
|
||||||
* Enabling CORS *
|
* Enabling CORS *
|
||||||
***********************************/
|
***********************************/
|
||||||
if (MODE === ModeType.Server || CORS === CorsType.ENABLED) {
|
if (CORS === CorsType.ENABLED) {
|
||||||
const whiteList: string[] = []
|
const whiteList: string[] = []
|
||||||
WHITELIST?.split(' ')
|
WHITELIST?.split(' ')
|
||||||
?.filter((url) => !!url)
|
?.filter((url) => !!url)
|
||||||
@@ -125,6 +123,7 @@ if (MODE === ModeType.Server) {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
app.use(express.json({ limit: '100mb' }))
|
app.use(express.json({ limit: '100mb' }))
|
||||||
app.use(express.static(path.join(__dirname, '../public')))
|
app.use(express.static(path.join(__dirname, '../public')))
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { Request, Security, Route, Tags, Post, Body } from 'tsoa'
|
import { Request, Security, Route, Tags, Post, Body } from 'tsoa'
|
||||||
import { ExecuteReturnJson, ExecutionController } from './internal'
|
import { ExecuteReturnJson, ExecutionController } from './internal'
|
||||||
import { PreProgramVars } from '../types'
|
|
||||||
import { ExecuteReturnJsonResponse } from '.'
|
import { ExecuteReturnJsonResponse } from '.'
|
||||||
import { getPreProgramVariables, parseLogToArray } from '../utils'
|
import {
|
||||||
|
getDesktopUserAutoExecPath,
|
||||||
|
getPreProgramVariables,
|
||||||
|
ModeType,
|
||||||
|
parseLogToArray
|
||||||
|
} from '../utils'
|
||||||
|
import { readFile } from '@sasjs/utils'
|
||||||
|
|
||||||
interface ExecuteSASCodePayload {
|
interface ExecuteSASCodePayload {
|
||||||
/**
|
/**
|
||||||
@@ -30,14 +35,23 @@ export class CodeController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const executeSASCode = async (req: any, { code }: ExecuteSASCodePayload) => {
|
const executeSASCode = async (
|
||||||
|
req: express.Request,
|
||||||
|
{ code }: ExecuteSASCodePayload
|
||||||
|
) => {
|
||||||
|
const { user } = req
|
||||||
|
const userAutoExec =
|
||||||
|
process.env.MODE === ModeType.Server
|
||||||
|
? user?.autoExec
|
||||||
|
: await readFile(getDesktopUserAutoExecPath())
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { webout, log, httpHeaders } =
|
const { webout, log, httpHeaders } =
|
||||||
(await new ExecutionController().executeProgram(
|
(await new ExecutionController().executeProgram(
|
||||||
code,
|
code,
|
||||||
getPreProgramVariables(req),
|
getPreProgramVariables(req),
|
||||||
{ ...req.query, _debug: 131 },
|
{ ...req.query, _debug: 131 },
|
||||||
undefined,
|
{ userAutoExec },
|
||||||
true
|
true
|
||||||
)) as ExecuteReturnJson
|
)) as ExecuteReturnJson
|
||||||
|
|
||||||
|
|||||||
@@ -119,6 +119,10 @@ filename _webout "${weboutPath}" mod;
|
|||||||
/* dynamic user-provided vars */
|
/* dynamic user-provided vars */
|
||||||
${preProgramVarStatments}
|
${preProgramVarStatments}
|
||||||
|
|
||||||
|
/* user autoexec starts */
|
||||||
|
${otherArgs?.userAutoExec ?? ''}
|
||||||
|
/* user autoexec ends */
|
||||||
|
|
||||||
/* actual job code */
|
/* actual job code */
|
||||||
${program}`
|
${program}`
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
|
import { Request, RequestHandler } from 'express'
|
||||||
import multer from 'multer'
|
import multer from 'multer'
|
||||||
import { uuidv4 } from '@sasjs/utils'
|
import { uuidv4 } from '@sasjs/utils'
|
||||||
import { getSessionController } from '.'
|
import { getSessionController } from '.'
|
||||||
|
|
||||||
export class FileUploadController {
|
export class FileUploadController {
|
||||||
private storage = multer.diskStorage({
|
private storage = multer.diskStorage({
|
||||||
destination: function (req: any, file: any, cb: any) {
|
destination: function (req: Request, file: any, cb: any) {
|
||||||
//Sending the intercepted files to the sessions subfolder
|
//Sending the intercepted files to the sessions subfolder
|
||||||
cb(null, req.sasSession.path)
|
cb(null, req.sasSession?.path)
|
||||||
},
|
},
|
||||||
filename: function (req: any, file: any, cb: any) {
|
filename: function (req: Request, file: any, cb: any) {
|
||||||
//req_file prefix + unique hash added to sas request files
|
//req_file prefix + unique hash added to sas request files
|
||||||
cb(null, `req_file_${uuidv4().replace(/-/gm, '')}`)
|
cb(null, `req_file_${uuidv4().replace(/-/gm, '')}`)
|
||||||
}
|
}
|
||||||
@@ -18,7 +19,7 @@ export class FileUploadController {
|
|||||||
|
|
||||||
//It will intercept request and generate unique uuid to be used as a subfolder name
|
//It will intercept request and generate unique uuid to be used as a subfolder name
|
||||||
//that will store the files uploaded
|
//that will store the files uploaded
|
||||||
public preUploadMiddleware = async (req: any, res: any, next: any) => {
|
public preUploadMiddleware: RequestHandler = async (req, res, next) => {
|
||||||
let session
|
let session
|
||||||
|
|
||||||
const sessionController = getSessionController()
|
const sessionController = getSessionController()
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ export class SessionController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const session = (req: any) => ({
|
const session = (req: express.Request) => ({
|
||||||
id: req.user.userId,
|
id: req.user!.userId,
|
||||||
username: req.user.username,
|
username: req.user!.username,
|
||||||
displayName: req.user.displayName
|
displayName: req.user!.displayName
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import {
|
|||||||
makeFilesNamesMap,
|
makeFilesNamesMap,
|
||||||
parseLogToArray
|
parseLogToArray
|
||||||
} from '../utils'
|
} from '../utils'
|
||||||
|
import { MulterFile } from '../types/Upload'
|
||||||
|
|
||||||
interface ExecuteReturnJsonPayload {
|
interface ExecuteReturnJsonPayload {
|
||||||
/**
|
/**
|
||||||
@@ -167,7 +168,7 @@ const executeReturnRaw = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const executeReturnJson = async (
|
const executeReturnJson = async (
|
||||||
req: any,
|
req: express.Request,
|
||||||
_program: string
|
_program: string
|
||||||
): Promise<ExecuteReturnJsonResponse> => {
|
): Promise<ExecuteReturnJsonResponse> => {
|
||||||
const sasCodePath =
|
const sasCodePath =
|
||||||
@@ -175,7 +176,9 @@ const executeReturnJson = async (
|
|||||||
.join(getFilesFolder(), _program)
|
.join(getFilesFolder(), _program)
|
||||||
.replace(new RegExp('/', 'g'), path.sep) + '.sas'
|
.replace(new RegExp('/', 'g'), path.sep) + '.sas'
|
||||||
|
|
||||||
const filesNamesMap = req.files?.length ? makeFilesNamesMap(req.files) : null
|
const filesNamesMap = req.files?.length
|
||||||
|
? makeFilesNamesMap(req.files as MulterFile[])
|
||||||
|
: null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { webout, log, httpHeaders } =
|
const { webout, log, httpHeaders } =
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import express from 'express'
|
||||||
import {
|
import {
|
||||||
Security,
|
Security,
|
||||||
Route,
|
Route,
|
||||||
@@ -10,7 +11,8 @@ import {
|
|||||||
Patch,
|
Patch,
|
||||||
Delete,
|
Delete,
|
||||||
Body,
|
Body,
|
||||||
Hidden
|
Hidden,
|
||||||
|
Request
|
||||||
} from 'tsoa'
|
} from 'tsoa'
|
||||||
|
|
||||||
import User, { UserPayload } from '../model/User'
|
import User, { UserPayload } from '../model/User'
|
||||||
@@ -27,6 +29,7 @@ interface UserDetailsResponse {
|
|||||||
username: string
|
username: string
|
||||||
isActive: boolean
|
isActive: boolean
|
||||||
isAdmin: boolean
|
isAdmin: boolean
|
||||||
|
autoExec?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
@Security('bearerAuth')
|
@Security('bearerAuth')
|
||||||
@@ -73,13 +76,19 @@ export class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Only Admin or user itself will get user autoExec code.
|
||||||
* @summary Get user properties - such as group memberships, userName, displayName.
|
* @summary Get user properties - such as group memberships, userName, displayName.
|
||||||
* @param userId The user's identifier
|
* @param userId The user's identifier
|
||||||
* @example userId 1234
|
* @example userId 1234
|
||||||
*/
|
*/
|
||||||
@Get('{userId}')
|
@Get('{userId}')
|
||||||
public async getUser(@Path() userId: number): Promise<UserDetailsResponse> {
|
public async getUser(
|
||||||
return getUser(userId)
|
@Request() req: express.Request,
|
||||||
|
@Path() userId: number
|
||||||
|
): Promise<UserDetailsResponse> {
|
||||||
|
const { user } = req
|
||||||
|
const getAutoExec = user!.isAdmin || user!.userId == userId
|
||||||
|
return getUser(userId, getAutoExec)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -123,7 +132,7 @@ const getAllUsers = async (): Promise<UserResponse[]> =>
|
|||||||
.exec()
|
.exec()
|
||||||
|
|
||||||
const createUser = async (data: UserPayload): Promise<UserDetailsResponse> => {
|
const createUser = async (data: UserPayload): Promise<UserDetailsResponse> => {
|
||||||
const { displayName, username, password, isAdmin, isActive } = data
|
const { displayName, username, password, isAdmin, isActive, autoExec } = data
|
||||||
|
|
||||||
// Checking if user is already in the database
|
// Checking if user is already in the database
|
||||||
const usernameExist = await User.findOne({ username })
|
const usernameExist = await User.findOne({ username })
|
||||||
@@ -138,7 +147,8 @@ const createUser = async (data: UserPayload): Promise<UserDetailsResponse> => {
|
|||||||
username,
|
username,
|
||||||
password: hashPassword,
|
password: hashPassword,
|
||||||
isAdmin,
|
isAdmin,
|
||||||
isActive
|
isActive,
|
||||||
|
autoExec
|
||||||
})
|
})
|
||||||
|
|
||||||
const savedUser = await user.save()
|
const savedUser = await user.save()
|
||||||
@@ -148,38 +158,42 @@ const createUser = async (data: UserPayload): Promise<UserDetailsResponse> => {
|
|||||||
displayName: savedUser.displayName,
|
displayName: savedUser.displayName,
|
||||||
username: savedUser.username,
|
username: savedUser.username,
|
||||||
isActive: savedUser.isActive,
|
isActive: savedUser.isActive,
|
||||||
isAdmin: savedUser.isAdmin
|
isAdmin: savedUser.isAdmin,
|
||||||
|
autoExec: savedUser.autoExec
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getUser = async (id: number): Promise<UserDetailsResponse> => {
|
const getUser = async (
|
||||||
|
id: number,
|
||||||
|
getAutoExec: boolean
|
||||||
|
): Promise<UserDetailsResponse> => {
|
||||||
const user = await User.findOne({ id })
|
const user = await User.findOne({ id })
|
||||||
.select({
|
|
||||||
_id: 0,
|
|
||||||
id: 1,
|
|
||||||
username: 1,
|
|
||||||
displayName: 1,
|
|
||||||
isAdmin: 1,
|
|
||||||
isActive: 1
|
|
||||||
})
|
|
||||||
.exec()
|
|
||||||
if (!user) throw new Error('User is not found.')
|
if (!user) throw new Error('User is not found.')
|
||||||
|
|
||||||
return user
|
return {
|
||||||
|
id: user.id,
|
||||||
|
displayName: user.displayName,
|
||||||
|
username: user.username,
|
||||||
|
isActive: user.isActive,
|
||||||
|
isAdmin: user.isAdmin,
|
||||||
|
autoExec: getAutoExec ? user.autoExec ?? '' : undefined
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateUser = async (
|
const updateUser = async (
|
||||||
id: number,
|
id: number,
|
||||||
data: UserPayload
|
data: Partial<UserPayload>
|
||||||
): Promise<UserDetailsResponse> => {
|
): Promise<UserDetailsResponse> => {
|
||||||
const { displayName, username, password, isAdmin, isActive } = data
|
const { displayName, username, password, isAdmin, isActive, autoExec } = data
|
||||||
|
|
||||||
const params: any = { displayName, isAdmin, isActive }
|
const params: any = { displayName, isAdmin, isActive, autoExec }
|
||||||
|
|
||||||
if (username) {
|
if (username) {
|
||||||
// Checking if user is already in the database
|
// Checking if user is already in the database
|
||||||
const usernameExist = await User.findOne({ username })
|
const usernameExist = await User.findOne({ username })
|
||||||
if (usernameExist?.id != id) throw new Error('Username already exists.')
|
if (usernameExist && usernameExist.id != id)
|
||||||
|
throw new Error('Username already exists.')
|
||||||
params.username = username
|
params.username = username
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,18 +203,17 @@ const updateUser = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updatedUser = await User.findOneAndUpdate({ id }, params, { new: true })
|
const updatedUser = await User.findOneAndUpdate({ id }, params, { new: true })
|
||||||
.select({
|
|
||||||
_id: 0,
|
|
||||||
id: 1,
|
|
||||||
username: 1,
|
|
||||||
displayName: 1,
|
|
||||||
isAdmin: 1,
|
|
||||||
isActive: 1
|
|
||||||
})
|
|
||||||
.exec()
|
|
||||||
if (!updatedUser) throw new Error('Unable to update user')
|
|
||||||
|
|
||||||
return updatedUser
|
if (!updatedUser) throw new Error(`Unable to find user with id: ${id}`)
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: updatedUser.id,
|
||||||
|
username: updatedUser.username,
|
||||||
|
displayName: updatedUser.displayName,
|
||||||
|
isAdmin: updatedUser.isAdmin,
|
||||||
|
isActive: updatedUser.isActive,
|
||||||
|
autoExec: updatedUser.autoExec
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteUser = async (
|
const deleteUser = async (
|
||||||
|
|||||||
@@ -90,12 +90,14 @@ const login = async (
|
|||||||
username: user.username,
|
username: user.username,
|
||||||
displayName: user.displayName,
|
displayName: user.displayName,
|
||||||
isAdmin: user.isAdmin,
|
isAdmin: user.isAdmin,
|
||||||
isActive: user.isActive
|
isActive: user.isActive,
|
||||||
|
autoExec: user.autoExec
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
loggedIn: true,
|
loggedIn: true,
|
||||||
user: {
|
user: {
|
||||||
|
id: user.id,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
displayName: user.displayName
|
displayName: user.displayName
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,27 @@
|
|||||||
|
import { RequestHandler, Request, Response, NextFunction } from 'express'
|
||||||
import jwt from 'jsonwebtoken'
|
import jwt from 'jsonwebtoken'
|
||||||
import { csrfProtection } from '../app'
|
import { csrfProtection } from '../app'
|
||||||
import { verifyTokenInDB } from '../utils'
|
import { fetchLatestAutoExec, verifyTokenInDB } from '../utils'
|
||||||
|
|
||||||
export const authenticateAccessToken = (req: any, res: any, next: any) => {
|
export const authenticateAccessToken: RequestHandler = async (
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
next
|
||||||
|
) => {
|
||||||
// if request is coming from web and has valid session
|
// if request is coming from web and has valid session
|
||||||
// we can validate the request and check for CSRF Token
|
// we can validate the request and check for CSRF Token
|
||||||
if (req.session?.loggedIn) {
|
if (req.session?.loggedIn) {
|
||||||
req.user = req.session.user
|
if (req.session.user) {
|
||||||
|
const user = await fetchLatestAutoExec(req.session.user)
|
||||||
|
|
||||||
return csrfProtection(req, res, next)
|
if (user) {
|
||||||
|
if (user.isActive) {
|
||||||
|
req.user = user
|
||||||
|
return csrfProtection(req, res, next)
|
||||||
|
} else return res.sendStatus(401)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res.sendStatus(401)
|
||||||
}
|
}
|
||||||
|
|
||||||
authenticateToken(
|
authenticateToken(
|
||||||
@@ -20,7 +33,7 @@ export const authenticateAccessToken = (req: any, res: any, next: any) => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const authenticateRefreshToken = (req: any, res: any, next: any) => {
|
export const authenticateRefreshToken: RequestHandler = (req, res, next) => {
|
||||||
authenticateToken(
|
authenticateToken(
|
||||||
req,
|
req,
|
||||||
res,
|
res,
|
||||||
@@ -31,16 +44,16 @@ export const authenticateRefreshToken = (req: any, res: any, next: any) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const authenticateToken = (
|
const authenticateToken = (
|
||||||
req: any,
|
req: Request,
|
||||||
res: any,
|
res: Response,
|
||||||
next: any,
|
next: NextFunction,
|
||||||
key: string,
|
key: string,
|
||||||
tokenType: 'accessToken' | 'refreshToken'
|
tokenType: 'accessToken' | 'refreshToken'
|
||||||
) => {
|
) => {
|
||||||
const { MODE } = process.env
|
const { MODE } = process.env
|
||||||
if (MODE?.trim() !== 'server') {
|
if (MODE?.trim() !== 'server') {
|
||||||
req.user = {
|
req.user = {
|
||||||
userId: '1234',
|
userId: 1234,
|
||||||
clientId: 'desktopModeClientId',
|
clientId: 'desktopModeClientId',
|
||||||
username: 'desktopModeUsername',
|
username: 'desktopModeUsername',
|
||||||
displayName: 'desktopModeDisplayName',
|
displayName: 'desktopModeDisplayName',
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
export const desktopRestrict = (req: any, res: any, next: any) => {
|
import { RequestHandler } from 'express'
|
||||||
|
|
||||||
|
export const desktopRestrict: RequestHandler = (req, res, next) => {
|
||||||
const { MODE } = process.env
|
const { MODE } = process.env
|
||||||
if (MODE?.trim() !== 'server')
|
if (MODE?.trim() !== 'server')
|
||||||
return res.status(403).send('Not Allowed while in Desktop Mode.')
|
return res.status(403).send('Not Allowed while in Desktop Mode.')
|
||||||
|
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
export const desktopUsername = (req: any, res: any, next: any) => {
|
export const desktopUsername: RequestHandler = (req, res, next) => {
|
||||||
const { MODE } = process.env
|
const { MODE } = process.env
|
||||||
if (MODE?.trim() !== 'server')
|
if (MODE?.trim() !== 'server')
|
||||||
return res.status(200).send({
|
return res.status(200).send({
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
export const verifyAdmin = (req: any, res: any, next: any) => {
|
import { RequestHandler } from 'express'
|
||||||
|
|
||||||
|
export const verifyAdmin: RequestHandler = (req, res, next) => {
|
||||||
const { MODE } = process.env
|
const { MODE } = process.env
|
||||||
if (MODE?.trim() !== 'server') return next()
|
if (MODE?.trim() !== 'server') return next()
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
export const verifyAdminIfNeeded = (req: any, res: any, next: any) => {
|
import { RequestHandler } from 'express'
|
||||||
|
|
||||||
|
export const verifyAdminIfNeeded: RequestHandler = (req, res, next) => {
|
||||||
const { user } = req
|
const { user } = req
|
||||||
const userId = parseInt(req.params.userId)
|
const userId = parseInt(req.params.userId)
|
||||||
|
|
||||||
if (!user.isAdmin && user.userId !== userId) {
|
if (!user?.isAdmin && user?.userId !== userId) {
|
||||||
return res.status(401).send('Admin account required')
|
return res.status(401).send('Admin account required')
|
||||||
}
|
}
|
||||||
next()
|
next()
|
||||||
|
|||||||
@@ -27,12 +27,18 @@ export interface UserPayload {
|
|||||||
* @example "true"
|
* @example "true"
|
||||||
*/
|
*/
|
||||||
isActive?: boolean
|
isActive?: boolean
|
||||||
|
/**
|
||||||
|
* User-specific auto-exec code
|
||||||
|
* @example ""
|
||||||
|
*/
|
||||||
|
autoExec?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IUserDocument extends UserPayload, Document {
|
interface IUserDocument extends UserPayload, Document {
|
||||||
id: number
|
id: number
|
||||||
isAdmin: boolean
|
isAdmin: boolean
|
||||||
isActive: boolean
|
isActive: boolean
|
||||||
|
autoExec: string
|
||||||
groups: Schema.Types.ObjectId[]
|
groups: Schema.Types.ObjectId[]
|
||||||
tokens: [{ [key: string]: string }]
|
tokens: [{ [key: string]: string }]
|
||||||
}
|
}
|
||||||
@@ -66,6 +72,9 @@ const userSchema = new Schema<IUserDocument>({
|
|||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: true
|
default: true
|
||||||
},
|
},
|
||||||
|
autoExec: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
groups: [{ type: Schema.Types.ObjectId, ref: 'Group' }],
|
groups: [{ type: Schema.Types.ObjectId, ref: 'Group' }],
|
||||||
tokens: [
|
tokens: [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -26,8 +26,11 @@ authRouter.post('/token', async (req, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
authRouter.post('/refresh', authenticateRefreshToken, async (req: any, res) => {
|
authRouter.post('/refresh', authenticateRefreshToken, async (req, res) => {
|
||||||
const userInfo: InfoJWT = req.user
|
const userInfo: InfoJWT = {
|
||||||
|
userId: req.user!.userId!,
|
||||||
|
clientId: req.user!.clientId!
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await controller.refresh(userInfo)
|
const response = await controller.refresh(userInfo)
|
||||||
@@ -38,8 +41,11 @@ authRouter.post('/refresh', authenticateRefreshToken, async (req: any, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
authRouter.delete('/logout', authenticateAccessToken, async (req: any, res) => {
|
authRouter.delete('/logout', authenticateAccessToken, async (req, res) => {
|
||||||
const userInfo: InfoJWT = req.user
|
const userInfo: InfoJWT = {
|
||||||
|
userId: req.user!.userId!,
|
||||||
|
clientId: req.user!.clientId!
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await controller.logout(userInfo)
|
await controller.logout(userInfo)
|
||||||
|
|||||||
@@ -33,12 +33,12 @@ groupRouter.get('/', authenticateAccessToken, async (req, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
groupRouter.get('/:groupId', authenticateAccessToken, async (req: any, res) => {
|
groupRouter.get('/:groupId', authenticateAccessToken, async (req, res) => {
|
||||||
const { groupId } = req.params
|
const { groupId } = req.params
|
||||||
|
|
||||||
const controller = new GroupController()
|
const controller = new GroupController()
|
||||||
try {
|
try {
|
||||||
const response = await controller.getGroup(groupId)
|
const response = await controller.getGroup(parseInt(groupId))
|
||||||
res.send(response)
|
res.send(response)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
@@ -49,12 +49,15 @@ groupRouter.post(
|
|||||||
'/:groupId/:userId',
|
'/:groupId/:userId',
|
||||||
authenticateAccessToken,
|
authenticateAccessToken,
|
||||||
verifyAdmin,
|
verifyAdmin,
|
||||||
async (req: any, res) => {
|
async (req, res) => {
|
||||||
const { groupId, userId } = req.params
|
const { groupId, userId } = req.params
|
||||||
|
|
||||||
const controller = new GroupController()
|
const controller = new GroupController()
|
||||||
try {
|
try {
|
||||||
const response = await controller.addUserToGroup(groupId, userId)
|
const response = await controller.addUserToGroup(
|
||||||
|
parseInt(groupId),
|
||||||
|
parseInt(userId)
|
||||||
|
)
|
||||||
res.send(response)
|
res.send(response)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
@@ -66,12 +69,15 @@ groupRouter.delete(
|
|||||||
'/:groupId/:userId',
|
'/:groupId/:userId',
|
||||||
authenticateAccessToken,
|
authenticateAccessToken,
|
||||||
verifyAdmin,
|
verifyAdmin,
|
||||||
async (req: any, res) => {
|
async (req, res) => {
|
||||||
const { groupId, userId } = req.params
|
const { groupId, userId } = req.params
|
||||||
|
|
||||||
const controller = new GroupController()
|
const controller = new GroupController()
|
||||||
try {
|
try {
|
||||||
const response = await controller.removeUserFromGroup(groupId, userId)
|
const response = await controller.removeUserFromGroup(
|
||||||
|
parseInt(groupId),
|
||||||
|
parseInt(userId)
|
||||||
|
)
|
||||||
res.send(response)
|
res.send(response)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
@@ -83,12 +89,12 @@ groupRouter.delete(
|
|||||||
'/:groupId',
|
'/:groupId',
|
||||||
authenticateAccessToken,
|
authenticateAccessToken,
|
||||||
verifyAdmin,
|
verifyAdmin,
|
||||||
async (req: any, res) => {
|
async (req, res) => {
|
||||||
const { groupId } = req.params
|
const { groupId } = req.params
|
||||||
|
|
||||||
const controller = new GroupController()
|
const controller = new GroupController()
|
||||||
try {
|
try {
|
||||||
await controller.deleteGroup(groupId)
|
await controller.deleteGroup(parseInt(groupId))
|
||||||
res.status(200).send('Group Deleted!')
|
res.status(200).send('Group Deleted!')
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ const user = {
|
|||||||
username: 'testUsername',
|
username: 'testUsername',
|
||||||
password: '87654321',
|
password: '87654321',
|
||||||
isAdmin: false,
|
isAdmin: false,
|
||||||
isActive: true
|
isActive: true,
|
||||||
|
autoExec: 'some sas code for auto exec;'
|
||||||
}
|
}
|
||||||
|
|
||||||
const controller = new UserController()
|
const controller = new UserController()
|
||||||
@@ -64,6 +65,7 @@ describe('user', () => {
|
|||||||
expect(res.body.displayName).toEqual(user.displayName)
|
expect(res.body.displayName).toEqual(user.displayName)
|
||||||
expect(res.body.isAdmin).toEqual(user.isAdmin)
|
expect(res.body.isAdmin).toEqual(user.isAdmin)
|
||||||
expect(res.body.isActive).toEqual(user.isActive)
|
expect(res.body.isActive).toEqual(user.isActive)
|
||||||
|
expect(res.body.autoExec).toEqual(user.autoExec)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should respond with Unauthorized if access token is not present', async () => {
|
it('should respond with Unauthorized if access token is not present', async () => {
|
||||||
@@ -360,7 +362,25 @@ describe('user', () => {
|
|||||||
await deleteAllUsers()
|
await deleteAllUsers()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should respond with user', async () => {
|
it('should respond with user autoExec when same user requests', async () => {
|
||||||
|
const dbUser = await controller.createUser(user)
|
||||||
|
const userId = dbUser.id
|
||||||
|
const accessToken = await generateAndSaveToken(userId)
|
||||||
|
|
||||||
|
const res = await request(app)
|
||||||
|
.get(`/SASjsApi/user/${userId}`)
|
||||||
|
.auth(accessToken, { type: 'bearer' })
|
||||||
|
.send()
|
||||||
|
.expect(200)
|
||||||
|
|
||||||
|
expect(res.body.username).toEqual(user.username)
|
||||||
|
expect(res.body.displayName).toEqual(user.displayName)
|
||||||
|
expect(res.body.isAdmin).toEqual(user.isAdmin)
|
||||||
|
expect(res.body.isActive).toEqual(user.isActive)
|
||||||
|
expect(res.body.autoExec).toEqual(user.autoExec)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should respond with user autoExec when admin user requests', async () => {
|
||||||
const dbUser = await controller.createUser(user)
|
const dbUser = await controller.createUser(user)
|
||||||
const userId = dbUser.id
|
const userId = dbUser.id
|
||||||
|
|
||||||
@@ -374,6 +394,7 @@ describe('user', () => {
|
|||||||
expect(res.body.displayName).toEqual(user.displayName)
|
expect(res.body.displayName).toEqual(user.displayName)
|
||||||
expect(res.body.isAdmin).toEqual(user.isAdmin)
|
expect(res.body.isAdmin).toEqual(user.isAdmin)
|
||||||
expect(res.body.isActive).toEqual(user.isActive)
|
expect(res.body.isActive).toEqual(user.isActive)
|
||||||
|
expect(res.body.autoExec).toEqual(user.autoExec)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should respond with user when access token is not of an admin account', async () => {
|
it('should respond with user when access token is not of an admin account', async () => {
|
||||||
@@ -395,6 +416,7 @@ describe('user', () => {
|
|||||||
expect(res.body.displayName).toEqual(user.displayName)
|
expect(res.body.displayName).toEqual(user.displayName)
|
||||||
expect(res.body.isAdmin).toEqual(user.isAdmin)
|
expect(res.body.isAdmin).toEqual(user.isAdmin)
|
||||||
expect(res.body.isActive).toEqual(user.isActive)
|
expect(res.body.isActive).toEqual(user.isActive)
|
||||||
|
expect(res.body.autoExec).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should respond with Unauthorized if access token is not present', async () => {
|
it('should respond with Unauthorized if access token is not present', async () => {
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ stpRouter.post(
|
|||||||
'/execute',
|
'/execute',
|
||||||
fileUploadController.preUploadMiddleware,
|
fileUploadController.preUploadMiddleware,
|
||||||
fileUploadController.getMulterUploadObject().any(),
|
fileUploadController.getMulterUploadObject().any(),
|
||||||
async (req: any, res: any) => {
|
async (req, res: any) => {
|
||||||
const { error: errQ, value: query } = executeProgramRawValidation(req.query)
|
const { error: errQ, value: query } = executeProgramRawValidation(req.query)
|
||||||
const { error: errB, value: body } = executeProgramRawValidation(req.body)
|
const { error: errB, value: body } = executeProgramRawValidation(req.body)
|
||||||
|
|
||||||
|
|||||||
@@ -36,12 +36,12 @@ userRouter.get('/', authenticateAccessToken, async (req, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
userRouter.get('/:userId', authenticateAccessToken, async (req: any, res) => {
|
userRouter.get('/:userId', authenticateAccessToken, async (req, res) => {
|
||||||
const { userId } = req.params
|
const { userId } = req.params
|
||||||
|
|
||||||
const controller = new UserController()
|
const controller = new UserController()
|
||||||
try {
|
try {
|
||||||
const response = await controller.getUser(userId)
|
const response = await controller.getUser(req, parseInt(userId))
|
||||||
res.send(response)
|
res.send(response)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
@@ -52,17 +52,17 @@ userRouter.patch(
|
|||||||
'/:userId',
|
'/:userId',
|
||||||
authenticateAccessToken,
|
authenticateAccessToken,
|
||||||
verifyAdminIfNeeded,
|
verifyAdminIfNeeded,
|
||||||
async (req: any, res) => {
|
async (req, res) => {
|
||||||
const { user } = req
|
const { user } = req
|
||||||
const { userId } = req.params
|
const { userId } = req.params
|
||||||
|
|
||||||
// only an admin can update `isActive` and `isAdmin` fields
|
// only an admin can update `isActive` and `isAdmin` fields
|
||||||
const { error, value: body } = updateUserValidation(req.body, user.isAdmin)
|
const { error, value: body } = updateUserValidation(req.body, user!.isAdmin)
|
||||||
if (error) return res.status(400).send(error.details[0].message)
|
if (error) return res.status(400).send(error.details[0].message)
|
||||||
|
|
||||||
const controller = new UserController()
|
const controller = new UserController()
|
||||||
try {
|
try {
|
||||||
const response = await controller.updateUser(userId, body)
|
const response = await controller.updateUser(parseInt(userId), body)
|
||||||
res.send(response)
|
res.send(response)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
@@ -74,17 +74,17 @@ userRouter.delete(
|
|||||||
'/:userId',
|
'/:userId',
|
||||||
authenticateAccessToken,
|
authenticateAccessToken,
|
||||||
verifyAdminIfNeeded,
|
verifyAdminIfNeeded,
|
||||||
async (req: any, res) => {
|
async (req, res) => {
|
||||||
const { user } = req
|
const { user } = req
|
||||||
const { userId } = req.params
|
const { userId } = req.params
|
||||||
|
|
||||||
// only an admin can delete user without providing password
|
// only an admin can delete user without providing password
|
||||||
const { error, value: data } = deleteUserValidation(req.body, user.isAdmin)
|
const { error, value: data } = deleteUserValidation(req.body, user!.isAdmin)
|
||||||
if (error) return res.status(400).send(error.details[0].message)
|
if (error) return res.status(400).send(error.details[0].message)
|
||||||
|
|
||||||
const controller = new UserController()
|
const controller = new UserController()
|
||||||
try {
|
try {
|
||||||
await controller.deleteUser(userId, data, user.isAdmin)
|
await controller.deleteUser(parseInt(userId), data, user!.isAdmin)
|
||||||
res.status(200).send('Account Deleted!')
|
res.status(200).send('Account Deleted!')
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
|
|||||||
9
api/src/types/RequestUser.ts
Normal file
9
api/src/types/RequestUser.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
export interface RequestUser {
|
||||||
|
userId: number
|
||||||
|
clientId: string
|
||||||
|
username: string
|
||||||
|
displayName: string
|
||||||
|
isAdmin: boolean
|
||||||
|
isActive: boolean
|
||||||
|
autoExec?: string
|
||||||
|
}
|
||||||
@@ -5,3 +5,4 @@ export * from './InfoJWT'
|
|||||||
export * from './PreProgramVars'
|
export * from './PreProgramVars'
|
||||||
export * from './Session'
|
export * from './Session'
|
||||||
export * from './TreeNode'
|
export * from './TreeNode'
|
||||||
|
export * from './RequestUser'
|
||||||
|
|||||||
9
api/src/types/system/express-session.d.ts
vendored
9
api/src/types/system/express-session.d.ts
vendored
@@ -2,13 +2,6 @@ import express from 'express'
|
|||||||
declare module 'express-session' {
|
declare module 'express-session' {
|
||||||
interface SessionData {
|
interface SessionData {
|
||||||
loggedIn: boolean
|
loggedIn: boolean
|
||||||
user: {
|
user: import('../').RequestUser
|
||||||
userId: number
|
|
||||||
clientId: string
|
|
||||||
username: string
|
|
||||||
displayName: string
|
|
||||||
isAdmin: boolean
|
|
||||||
isActive: boolean
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
api/src/types/system/express.d.ts
vendored
Normal file
7
api/src/types/system/express.d.ts
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
declare namespace Express {
|
||||||
|
export interface Request {
|
||||||
|
accessToken?: string
|
||||||
|
user?: import('../').RequestUser
|
||||||
|
sasSession?: import('../').Session
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
import { homedir } from 'os'
|
||||||
|
|
||||||
export const apiRoot = path.join(__dirname, '..', '..')
|
export const apiRoot = path.join(__dirname, '..', '..')
|
||||||
export const codebaseRoot = path.join(apiRoot, '..')
|
export const codebaseRoot = path.join(apiRoot, '..')
|
||||||
@@ -13,6 +14,11 @@ export const sasJSCoreMacrosInfo = path.join(sasJSCoreMacros, '.macrolist')
|
|||||||
|
|
||||||
export const getWebBuildFolder = () => path.join(codebaseRoot, 'web', 'build')
|
export const getWebBuildFolder = () => path.join(codebaseRoot, 'web', 'build')
|
||||||
|
|
||||||
|
export const getSasjsHomeFolder = () => path.join(homedir(), '.sasjs-server')
|
||||||
|
|
||||||
|
export const getDesktopUserAutoExecPath = () =>
|
||||||
|
path.join(getSasjsHomeFolder(), 'user-autoexec.sas')
|
||||||
|
|
||||||
export const getSasjsRootFolder = () => process.driveLoc
|
export const getSasjsRootFolder = () => process.driveLoc
|
||||||
|
|
||||||
export const getAppStreamConfigPath = () =>
|
export const getAppStreamConfigPath = () =>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
|
import { Request } from 'express'
|
||||||
import { PreProgramVars } from '../types'
|
import { PreProgramVars } from '../types'
|
||||||
|
|
||||||
export const getPreProgramVariables = (req: any): PreProgramVars => {
|
export const getPreProgramVariables = (req: Request): PreProgramVars => {
|
||||||
const host = req.get('host')
|
const host = req.get('host')
|
||||||
const protocol = req.protocol + '://'
|
const protocol = req.protocol + '://'
|
||||||
const { user, accessToken } = req
|
const { user, accessToken } = req
|
||||||
@@ -20,9 +21,9 @@ export const getPreProgramVariables = (req: any): PreProgramVars => {
|
|||||||
if (cookies.length) httpHeaders.push(`cookie: ${cookies.join('; ')}`)
|
if (cookies.length) httpHeaders.push(`cookie: ${cookies.join('; ')}`)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
username: user.username,
|
username: user!.username,
|
||||||
userId: user.userId,
|
userId: user!.userId,
|
||||||
displayName: user.displayName,
|
displayName: user!.displayName,
|
||||||
serverUrl: protocol + host,
|
serverUrl: protocol + host,
|
||||||
httpHeaders
|
httpHeaders
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,14 @@
|
|||||||
import { createFolder } from '@sasjs/utils'
|
import { createFile, createFolder, fileExists } from '@sasjs/utils'
|
||||||
import { getFilesFolder } from './file'
|
import { getDesktopUserAutoExecPath, getFilesFolder } from './file'
|
||||||
|
import { ModeType } from './verifyEnvVariables'
|
||||||
|
|
||||||
export const setupFolders = async () => {
|
export const setupFolders = async () => {
|
||||||
const drivePath = getFilesFolder()
|
const drivePath = getFilesFolder()
|
||||||
await createFolder(drivePath)
|
await createFolder(drivePath)
|
||||||
|
|
||||||
|
if (process.env.MODE === ModeType.Desktop) {
|
||||||
|
if (!(await fileExists(getDesktopUserAutoExecPath()))) {
|
||||||
|
await createFile(getDesktopUserAutoExecPath(), '')
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,8 @@ export const registerUserValidation = (data: any): Joi.ValidationResult =>
|
|||||||
username: usernameSchema.required(),
|
username: usernameSchema.required(),
|
||||||
password: passwordSchema.required(),
|
password: passwordSchema.required(),
|
||||||
isAdmin: Joi.boolean(),
|
isAdmin: Joi.boolean(),
|
||||||
isActive: Joi.boolean()
|
isActive: Joi.boolean(),
|
||||||
|
autoExec: Joi.string().allow('')
|
||||||
}).validate(data)
|
}).validate(data)
|
||||||
|
|
||||||
export const deleteUserValidation = (
|
export const deleteUserValidation = (
|
||||||
@@ -57,7 +58,8 @@ export const updateUserValidation = (
|
|||||||
const validationChecks: any = {
|
const validationChecks: any = {
|
||||||
displayName: Joi.string().min(6),
|
displayName: Joi.string().min(6),
|
||||||
username: usernameSchema,
|
username: usernameSchema,
|
||||||
password: passwordSchema
|
password: passwordSchema,
|
||||||
|
autoExec: Joi.string().allow('')
|
||||||
}
|
}
|
||||||
if (isAdmin) {
|
if (isAdmin) {
|
||||||
validationChecks.isAdmin = Joi.boolean()
|
validationChecks.isAdmin = Joi.boolean()
|
||||||
|
|||||||
@@ -1,11 +1,30 @@
|
|||||||
import User from '../model/User'
|
import User from '../model/User'
|
||||||
|
import { RequestUser } from '../types'
|
||||||
|
|
||||||
|
export const fetchLatestAutoExec = async (
|
||||||
|
reqUser: RequestUser
|
||||||
|
): Promise<RequestUser | undefined> => {
|
||||||
|
const dbUser = await User.findOne({ id: reqUser.userId })
|
||||||
|
|
||||||
|
if (!dbUser) return undefined
|
||||||
|
|
||||||
|
return {
|
||||||
|
userId: reqUser.userId,
|
||||||
|
clientId: reqUser.clientId,
|
||||||
|
username: dbUser.username,
|
||||||
|
displayName: dbUser.displayName,
|
||||||
|
isAdmin: dbUser.isAdmin,
|
||||||
|
isActive: dbUser.isActive,
|
||||||
|
autoExec: dbUser.autoExec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const verifyTokenInDB = async (
|
export const verifyTokenInDB = async (
|
||||||
userId: number,
|
userId: number,
|
||||||
clientId: string,
|
clientId: string,
|
||||||
token: string,
|
token: string,
|
||||||
tokenType: 'accessToken' | 'refreshToken'
|
tokenType: 'accessToken' | 'refreshToken'
|
||||||
) => {
|
): Promise<RequestUser | undefined> => {
|
||||||
const dbUser = await User.findOne({ id: userId })
|
const dbUser = await User.findOne({ id: userId })
|
||||||
|
|
||||||
if (!dbUser) return undefined
|
if (!dbUser) return undefined
|
||||||
@@ -21,7 +40,8 @@ export const verifyTokenInDB = async (
|
|||||||
username: dbUser.username,
|
username: dbUser.username,
|
||||||
displayName: dbUser.displayName,
|
displayName: dbUser.displayName,
|
||||||
isAdmin: dbUser.isAdmin,
|
isAdmin: dbUser.isAdmin,
|
||||||
isActive: dbUser.isActive
|
isActive: dbUser.isActive,
|
||||||
|
autoExec: dbUser.autoExec
|
||||||
}
|
}
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,11 @@ import Header from './components/header'
|
|||||||
import Home from './components/home'
|
import Home from './components/home'
|
||||||
import Drive from './containers/Drive'
|
import Drive from './containers/Drive'
|
||||||
import Studio from './containers/Studio'
|
import Studio from './containers/Studio'
|
||||||
|
import Settings from './containers/Settings'
|
||||||
|
|
||||||
import { AppContext } from './context/appContext'
|
import { AppContext } from './context/appContext'
|
||||||
import AuthCode from './containers/AuthCode'
|
import AuthCode from './containers/AuthCode'
|
||||||
|
import { ToastContainer } from 'react-toastify'
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const appContext = useContext(AppContext)
|
const appContext = useContext(AppContext)
|
||||||
@@ -44,10 +46,14 @@ function App() {
|
|||||||
<Route exact path="/SASjsStudio">
|
<Route exact path="/SASjsStudio">
|
||||||
<Studio />
|
<Studio />
|
||||||
</Route>
|
</Route>
|
||||||
|
<Route exact path="/SASjsSettings">
|
||||||
|
<Settings />
|
||||||
|
</Route>
|
||||||
<Route exact path="/SASjsLogon">
|
<Route exact path="/SASjsLogon">
|
||||||
<AuthCode />
|
<AuthCode />
|
||||||
</Route>
|
</Route>
|
||||||
</Switch>
|
</Switch>
|
||||||
|
<ToastContainer />
|
||||||
</HashRouter>
|
</HashRouter>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, { useState, useContext } from 'react'
|
import React, { useState, useEffect, useContext } from 'react'
|
||||||
import { Link, useHistory, useLocation } from 'react-router-dom'
|
import { Link, useHistory, useLocation } from 'react-router-dom'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
MenuItem
|
MenuItem
|
||||||
} from '@mui/material'
|
} from '@mui/material'
|
||||||
import OpenInNewIcon from '@mui/icons-material/OpenInNew'
|
import OpenInNewIcon from '@mui/icons-material/OpenInNew'
|
||||||
|
import SettingsIcon from '@mui/icons-material/Settings'
|
||||||
|
|
||||||
import Username from './username'
|
import Username from './username'
|
||||||
import { AppContext } from '../context/appContext'
|
import { AppContext } from '../context/appContext'
|
||||||
@@ -20,17 +21,23 @@ const PORT_API = process.env.PORT_API
|
|||||||
const baseUrl =
|
const baseUrl =
|
||||||
NODE_ENV === 'development' ? `http://localhost:${PORT_API ?? 5000}` : ''
|
NODE_ENV === 'development' ? `http://localhost:${PORT_API ?? 5000}` : ''
|
||||||
|
|
||||||
|
const validTabs = ['/', '/SASjsDrive', '/SASjsStudio']
|
||||||
|
|
||||||
const Header = (props: any) => {
|
const Header = (props: any) => {
|
||||||
const history = useHistory()
|
const history = useHistory()
|
||||||
const { pathname } = useLocation()
|
const { pathname } = useLocation()
|
||||||
const appContext = useContext(AppContext)
|
const appContext = useContext(AppContext)
|
||||||
const [tabValue, setTabValue] = useState(
|
const [tabValue, setTabValue] = useState(
|
||||||
pathname === '/SASjsLogon' ? '/' : pathname
|
validTabs.includes(pathname) ? pathname : '/'
|
||||||
)
|
)
|
||||||
const [anchorEl, setAnchorEl] = useState<
|
const [anchorEl, setAnchorEl] = useState<
|
||||||
(EventTarget & HTMLButtonElement) | null
|
(EventTarget & HTMLButtonElement) | null
|
||||||
>(null)
|
>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setTabValue(validTabs.includes(pathname) ? pathname : '/')
|
||||||
|
}, [pathname])
|
||||||
|
|
||||||
const handleMenu = (
|
const handleMenu = (
|
||||||
event: React.MouseEvent<HTMLButtonElement, MouseEvent>
|
event: React.MouseEvent<HTMLButtonElement, MouseEvent>
|
||||||
) => {
|
) => {
|
||||||
@@ -46,7 +53,10 @@ const Header = (props: any) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const handleLogout = () => {
|
const handleLogout = () => {
|
||||||
if (appContext.logout) appContext.logout()
|
if (appContext.logout) {
|
||||||
|
handleClose()
|
||||||
|
appContext.logout()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<AppBar
|
<AppBar
|
||||||
@@ -134,6 +144,18 @@ const Header = (props: any) => {
|
|||||||
open={!!anchorEl}
|
open={!!anchorEl}
|
||||||
onClose={handleClose}
|
onClose={handleClose}
|
||||||
>
|
>
|
||||||
|
<MenuItem sx={{ justifyContent: 'center' }}>
|
||||||
|
<Button
|
||||||
|
component={Link}
|
||||||
|
to="/SASjsSettings"
|
||||||
|
onClick={handleClose}
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
startIcon={<SettingsIcon />}
|
||||||
|
>
|
||||||
|
Setting
|
||||||
|
</Button>
|
||||||
|
</MenuItem>
|
||||||
<MenuItem onClick={handleLogout} sx={{ justifyContent: 'center' }}>
|
<MenuItem onClick={handleLogout} sx={{ justifyContent: 'center' }}>
|
||||||
<Button variant="contained" color="primary">
|
<Button variant="contained" color="primary">
|
||||||
Logout
|
Logout
|
||||||
|
|||||||
@@ -27,9 +27,10 @@ const Login = () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (loggedIn) {
|
if (loggedIn) {
|
||||||
appContext.setLoggedIn?.(loggedIn)
|
appContext.setUserId?.(user.id)
|
||||||
appContext.setUsername?.(user.username)
|
appContext.setUsername?.(user.username)
|
||||||
appContext.setDisplayName?.(user.displayName)
|
appContext.setDisplayName?.(user.displayName)
|
||||||
|
appContext.setLoggedIn?.(loggedIn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { CopyToClipboard } from 'react-copy-to-clipboard'
|
import { CopyToClipboard } from 'react-copy-to-clipboard'
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { ToastContainer, toast } from 'react-toastify'
|
import { toast } from 'react-toastify'
|
||||||
import 'react-toastify/dist/ReactToastify.css'
|
import 'react-toastify/dist/ReactToastify.css'
|
||||||
import { useLocation } from 'react-router-dom'
|
import { useLocation } from 'react-router-dom'
|
||||||
|
|
||||||
@@ -71,8 +71,6 @@ const AuthCode = () => {
|
|||||||
>
|
>
|
||||||
<Button variant="contained">Copy to Clipboard</Button>
|
<Button variant="contained">Copy to Clipboard</Button>
|
||||||
</CopyToClipboard>
|
</CopyToClipboard>
|
||||||
|
|
||||||
<ToastContainer />
|
|
||||||
</Box>
|
</Box>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
55
web/src/containers/Settings/index.tsx
Normal file
55
web/src/containers/Settings/index.tsx
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import * as React from 'react'
|
||||||
|
|
||||||
|
import { Box, Paper, Tab, styled } from '@mui/material'
|
||||||
|
import TabContext from '@mui/lab/TabContext'
|
||||||
|
import TabList from '@mui/lab/TabList'
|
||||||
|
import TabPanel from '@mui/lab/TabPanel'
|
||||||
|
|
||||||
|
import Profile from './profile'
|
||||||
|
|
||||||
|
const StyledTab = styled(Tab)({
|
||||||
|
background: 'black',
|
||||||
|
margin: '0 5px 5px 0'
|
||||||
|
})
|
||||||
|
|
||||||
|
const StyledTabpanel = styled(TabPanel)({
|
||||||
|
flexGrow: 1
|
||||||
|
})
|
||||||
|
|
||||||
|
const Settings = () => {
|
||||||
|
const [value, setValue] = React.useState('profile')
|
||||||
|
|
||||||
|
const handleChange = (event: React.SyntheticEvent, newValue: string) => {
|
||||||
|
setValue(newValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: 'flex',
|
||||||
|
marginTop: '65px'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<TabContext value={value}>
|
||||||
|
<Box component={Paper} sx={{ margin: '0 5px', height: '92vh' }}>
|
||||||
|
<TabList
|
||||||
|
TabIndicatorProps={{
|
||||||
|
style: {
|
||||||
|
display: 'none'
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
orientation="vertical"
|
||||||
|
onChange={handleChange}
|
||||||
|
>
|
||||||
|
<StyledTab label="Profile" value="profile" />
|
||||||
|
</TabList>
|
||||||
|
</Box>
|
||||||
|
<StyledTabpanel value="profile">
|
||||||
|
<Profile />
|
||||||
|
</StyledTabpanel>
|
||||||
|
</TabContext>
|
||||||
|
</Box>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Settings
|
||||||
148
web/src/containers/Settings/profile.tsx
Normal file
148
web/src/containers/Settings/profile.tsx
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
import React, { useState, useEffect, useContext } from 'react'
|
||||||
|
import axios from 'axios'
|
||||||
|
import {
|
||||||
|
Grid,
|
||||||
|
CircularProgress,
|
||||||
|
Card,
|
||||||
|
CardHeader,
|
||||||
|
Divider,
|
||||||
|
CardContent,
|
||||||
|
TextField,
|
||||||
|
CardActions,
|
||||||
|
Button,
|
||||||
|
FormGroup,
|
||||||
|
FormControlLabel,
|
||||||
|
Checkbox
|
||||||
|
} from '@mui/material'
|
||||||
|
|
||||||
|
import { AppContext } from '../../context/appContext'
|
||||||
|
import { toast } from 'react-toastify'
|
||||||
|
|
||||||
|
const Profile = () => {
|
||||||
|
const [isLoading, setIsLoading] = useState(false)
|
||||||
|
const appContext = useContext(AppContext)
|
||||||
|
const [user, setUser] = useState({} as any)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setIsLoading(true)
|
||||||
|
axios
|
||||||
|
.get(`/SASjsApi/user/${appContext.userId}`)
|
||||||
|
.then((res: any) => {
|
||||||
|
setUser(res.data)
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err)
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
setIsLoading(false)
|
||||||
|
})
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const handleChange = (event: any) => {
|
||||||
|
const { name, value } = event.target
|
||||||
|
|
||||||
|
setUser({ ...user, [name]: value })
|
||||||
|
}
|
||||||
|
const handleSubmit = () => {
|
||||||
|
setIsLoading(true)
|
||||||
|
axios
|
||||||
|
.patch(`/SASjsApi/user/${appContext.userId}`, {
|
||||||
|
username: user.username,
|
||||||
|
displayName: user.displayName,
|
||||||
|
autoExec: user.autoExec
|
||||||
|
})
|
||||||
|
.then((res: any) => {
|
||||||
|
toast.success('User information updated', {
|
||||||
|
theme: 'dark',
|
||||||
|
position: toast.POSITION.BOTTOM_RIGHT
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
toast.error('Failed: ' + err.response?.data || err.text, {
|
||||||
|
theme: 'dark',
|
||||||
|
position: toast.POSITION.BOTTOM_RIGHT
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
setIsLoading(false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return isLoading ? (
|
||||||
|
<CircularProgress
|
||||||
|
style={{ position: 'absolute', left: '50%', top: '50%' }}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<Card>
|
||||||
|
<CardHeader title="Profile Information" />
|
||||||
|
<Divider />
|
||||||
|
<CardContent>
|
||||||
|
<Grid container spacing={4}>
|
||||||
|
<Grid item md={6} xs={12}>
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
error={user.displayName?.length === 0}
|
||||||
|
helperText="Please specify display name"
|
||||||
|
label="Display Name"
|
||||||
|
name="displayName"
|
||||||
|
onChange={handleChange}
|
||||||
|
required
|
||||||
|
value={user.displayName}
|
||||||
|
variant="outlined"
|
||||||
|
/>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
<Grid item md={6} xs={12}>
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
error={user.username?.length === 0}
|
||||||
|
helperText="Please specify username"
|
||||||
|
label="Username"
|
||||||
|
name="username"
|
||||||
|
onChange={handleChange}
|
||||||
|
required
|
||||||
|
value={user.username}
|
||||||
|
variant="outlined"
|
||||||
|
/>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
<Grid item xs={6}>
|
||||||
|
<FormGroup row>
|
||||||
|
<FormControlLabel
|
||||||
|
disabled
|
||||||
|
control={<Checkbox checked={user.isActive} />}
|
||||||
|
label="isActive"
|
||||||
|
/>
|
||||||
|
<FormControlLabel
|
||||||
|
disabled
|
||||||
|
control={<Checkbox checked={user.isAdmin} />}
|
||||||
|
label="isAdmin"
|
||||||
|
/>
|
||||||
|
</FormGroup>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
<Grid item xs={12}>
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
label="autoExec"
|
||||||
|
name="autoExec"
|
||||||
|
onChange={handleChange}
|
||||||
|
multiline
|
||||||
|
rows="4"
|
||||||
|
value={user.autoExec}
|
||||||
|
variant="outlined"
|
||||||
|
/>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</CardContent>
|
||||||
|
<Divider />
|
||||||
|
<CardActions>
|
||||||
|
<Button type="submit" variant="contained" onClick={handleSubmit}>
|
||||||
|
Save Changes
|
||||||
|
</Button>
|
||||||
|
</CardActions>
|
||||||
|
</Card>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Profile
|
||||||
@@ -13,6 +13,8 @@ interface AppContextProps {
|
|||||||
checkingSession: boolean
|
checkingSession: boolean
|
||||||
loggedIn: boolean
|
loggedIn: boolean
|
||||||
setLoggedIn: Dispatch<SetStateAction<boolean>> | null
|
setLoggedIn: Dispatch<SetStateAction<boolean>> | null
|
||||||
|
userId: number
|
||||||
|
setUserId: Dispatch<SetStateAction<number>> | null
|
||||||
username: string
|
username: string
|
||||||
setUsername: Dispatch<SetStateAction<string>> | null
|
setUsername: Dispatch<SetStateAction<string>> | null
|
||||||
displayName: string
|
displayName: string
|
||||||
@@ -24,6 +26,8 @@ export const AppContext = createContext<AppContextProps>({
|
|||||||
checkingSession: false,
|
checkingSession: false,
|
||||||
loggedIn: false,
|
loggedIn: false,
|
||||||
setLoggedIn: null,
|
setLoggedIn: null,
|
||||||
|
userId: 0,
|
||||||
|
setUserId: null,
|
||||||
username: '',
|
username: '',
|
||||||
setUsername: null,
|
setUsername: null,
|
||||||
displayName: '',
|
displayName: '',
|
||||||
@@ -35,6 +39,7 @@ const AppContextProvider = (props: { children: ReactNode }) => {
|
|||||||
const { children } = props
|
const { children } = props
|
||||||
const [checkingSession, setCheckingSession] = useState(false)
|
const [checkingSession, setCheckingSession] = useState(false)
|
||||||
const [loggedIn, setLoggedIn] = useState(false)
|
const [loggedIn, setLoggedIn] = useState(false)
|
||||||
|
const [userId, setUserId] = useState(0)
|
||||||
const [username, setUsername] = useState('')
|
const [username, setUsername] = useState('')
|
||||||
const [displayName, setDisplayName] = useState('')
|
const [displayName, setDisplayName] = useState('')
|
||||||
|
|
||||||
@@ -46,9 +51,10 @@ const AppContextProvider = (props: { children: ReactNode }) => {
|
|||||||
.then((res) => res.data)
|
.then((res) => res.data)
|
||||||
.then((data: any) => {
|
.then((data: any) => {
|
||||||
setCheckingSession(false)
|
setCheckingSession(false)
|
||||||
setLoggedIn(true)
|
setUserId(data.id)
|
||||||
setUsername(data.username)
|
setUsername(data.username)
|
||||||
setDisplayName(data.displayName)
|
setDisplayName(data.displayName)
|
||||||
|
setLoggedIn(true)
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
setLoggedIn(false)
|
setLoggedIn(false)
|
||||||
@@ -70,6 +76,8 @@ const AppContextProvider = (props: { children: ReactNode }) => {
|
|||||||
checkingSession,
|
checkingSession,
|
||||||
loggedIn,
|
loggedIn,
|
||||||
setLoggedIn,
|
setLoggedIn,
|
||||||
|
userId,
|
||||||
|
setUserId,
|
||||||
username,
|
username,
|
||||||
setUsername,
|
setUsername,
|
||||||
displayName,
|
displayName,
|
||||||
|
|||||||
Reference in New Issue
Block a user