@driveflux/auth 4.0.58 → 4.0.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/AuthProvider.js +59 -76
- package/dist/authorization/constants.js +24 -45
- package/dist/authorization/define.js +28 -57
- package/dist/authorization/fields/index.js +7 -4
- package/dist/authorization/helpers.js +8 -10
- package/dist/authorization/index.js +6 -6
- package/dist/authorization/permissions-list.js +7 -5
- package/dist/authorization/quick.js +1 -1
- package/dist/authorization/roles/admin/business-development-executive.js +7 -20
- package/dist/authorization/roles/admin/ceo.js +2 -4
- package/dist/authorization/roles/admin/common.js +2 -4
- package/dist/authorization/roles/admin/concierge.js +10 -35
- package/dist/authorization/roles/admin/customer-success-executive.js +10 -40
- package/dist/authorization/roles/admin/data-analyst.js +4 -7
- package/dist/authorization/roles/admin/designer.js +4 -7
- package/dist/authorization/roles/admin/engineer.js +4 -7
- package/dist/authorization/roles/admin/finance-executive.js +4 -11
- package/dist/authorization/roles/admin/head-of-business-development.js +4 -14
- package/dist/authorization/roles/admin/head-of-data-analytics.js +4 -14
- package/dist/authorization/roles/admin/head-of-engineering.js +6 -17
- package/dist/authorization/roles/admin/head-of-finance.js +3 -8
- package/dist/authorization/roles/admin/head-of-human-resources.js +5 -13
- package/dist/authorization/roles/admin/head-of-marketing.js +5 -17
- package/dist/authorization/roles/admin/head-of-operations.js +3 -8
- package/dist/authorization/roles/admin/head-of-product.js +6 -17
- package/dist/authorization/roles/admin/head-of-sales.js +5 -17
- package/dist/authorization/roles/admin/human-resources-executive.js +5 -12
- package/dist/authorization/roles/admin/marketing-executive.js +4 -7
- package/dist/authorization/roles/admin/product-manager.js +4 -7
- package/dist/authorization/roles/admin/sales-executive.js +8 -24
- package/dist/authorization/roles/consumer/business-admin.js +6 -19
- package/dist/authorization/roles/consumer/business-user.js +6 -18
- package/dist/authorization/roles/consumer/member.js +6 -16
- package/dist/authorization/types.js +1 -1
- package/dist/authorization/update-user-permissions.js +15 -22
- package/dist/authorization/utils.js +11 -26
- package/dist/context.js +9 -8
- package/dist/default.js +1 -1
- package/dist/server/authenticate-user.js +7 -11
- package/dist/server/cors.js +12 -23
- package/dist/server/credentials-provider.js +2 -2
- package/dist/server/next-auth.js +109 -104
- package/dist/server/prisma-adapter.js +52 -88
- package/dist/server/verfiy-token.js +24 -39
- package/dist/translations.js +4 -4
- package/dist/use-auth.js +1 -1
- package/dist/use-session.js +1 -1
- package/package.json +7 -7
|
@@ -1,25 +1,12 @@
|
|
|
1
|
-
export const defineRoleAbilitiesBusinessAdmin = async (can, user)=>{
|
|
1
|
+
export const defineRoleAbilitiesBusinessAdmin = async (can, user) => {
|
|
2
2
|
if (!user.businessId) {
|
|
3
3
|
return;
|
|
4
4
|
}
|
|
5
|
-
can([
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
businessId: user.businessId
|
|
10
|
-
});
|
|
11
|
-
can('read', 'Subscription', {
|
|
12
|
-
businessId: user.businessId
|
|
13
|
-
});
|
|
14
|
-
can('update', 'Business', [
|
|
15
|
-
'phoneNumber',
|
|
16
|
-
'mobileNumber',
|
|
17
|
-
'addresses'
|
|
18
|
-
], {
|
|
19
|
-
id: user.businessId
|
|
20
|
-
});
|
|
21
|
-
can('read', 'Business', {
|
|
22
|
-
id: user.businessId
|
|
5
|
+
can(['read', 'update'], 'User', { businessId: user.businessId });
|
|
6
|
+
can('read', 'Subscription', { businessId: user.businessId });
|
|
7
|
+
can('update', 'Business', ['phoneNumber', 'mobileNumber', 'addresses'], {
|
|
8
|
+
id: user.businessId,
|
|
23
9
|
});
|
|
10
|
+
can('read', 'Business', { id: user.businessId });
|
|
24
11
|
can('create', 'Mandate');
|
|
25
12
|
};
|
|
@@ -1,24 +1,12 @@
|
|
|
1
|
-
export const defineRoleAbilitiesBusinessUser = async (can, user)=>{
|
|
1
|
+
export const defineRoleAbilitiesBusinessUser = async (can, user) => {
|
|
2
2
|
if (!user.businessId) {
|
|
3
3
|
return;
|
|
4
4
|
}
|
|
5
|
-
can([
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
});
|
|
10
|
-
can('read', 'Subscription', {
|
|
11
|
-
businessId: user.businessId
|
|
12
|
-
});
|
|
13
|
-
can('update', 'Business', [
|
|
14
|
-
'phoneNumber',
|
|
15
|
-
'mobileNumber',
|
|
16
|
-
'addresses'
|
|
17
|
-
], {
|
|
18
|
-
id: user.businessId
|
|
19
|
-
});
|
|
20
|
-
can('read', 'Business', {
|
|
21
|
-
id: user.businessId
|
|
5
|
+
can(['read'], 'User', { businessId: user.businessId });
|
|
6
|
+
can('read', 'Subscription', { businessId: user.businessId });
|
|
7
|
+
can('update', 'Business', ['phoneNumber', 'mobileNumber', 'addresses'], {
|
|
8
|
+
id: user.businessId,
|
|
22
9
|
});
|
|
10
|
+
can('read', 'Business', { id: user.businessId });
|
|
23
11
|
can('create', 'Mandate');
|
|
24
12
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const defineRoleAbilitiesMember = async (can, user)=>{
|
|
1
|
+
export const defineRoleAbilitiesMember = async (can, user) => {
|
|
2
2
|
can('read', 'Cycle', [
|
|
3
3
|
'id',
|
|
4
4
|
'startDate',
|
|
@@ -6,23 +6,13 @@ export const defineRoleAbilitiesMember = async (can, user)=>{
|
|
|
6
6
|
'utilization.pricePerKm',
|
|
7
7
|
'utilization.paid',
|
|
8
8
|
'utilization.allowedMileage',
|
|
9
|
-
'utilization.mileage'
|
|
10
|
-
], {
|
|
11
|
-
|
|
12
|
-
userId: user.id
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
can('read', 'Vehicle', {
|
|
16
|
-
status: 'listed'
|
|
17
|
-
});
|
|
9
|
+
'utilization.mileage',
|
|
10
|
+
], { subscription: { userId: user.id } });
|
|
11
|
+
can('read', 'Vehicle', { status: 'listed' });
|
|
18
12
|
can('create', 'Business');
|
|
19
13
|
if (user.businessId) {
|
|
20
|
-
can('update', 'Business', {
|
|
21
|
-
id: user.businessId
|
|
22
|
-
});
|
|
14
|
+
can('update', 'Business', { id: user.businessId });
|
|
23
15
|
}
|
|
24
|
-
can('read', 'Subscription', {
|
|
25
|
-
userId: user.id
|
|
26
|
-
});
|
|
16
|
+
can('read', 'Subscription', { userId: user.id });
|
|
27
17
|
can('read', 'Coupon');
|
|
28
18
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {};
|
|
@@ -2,39 +2,32 @@ import { prisma } from '@driveflux/db';
|
|
|
2
2
|
import { makeProblem, PROBLEM_NOT_FOUND } from '@driveflux/problem';
|
|
3
3
|
import { Err, Ok } from '@driveflux/result';
|
|
4
4
|
import { defineAbilityFor } from './define.js';
|
|
5
|
-
export const updateUserPermissions = async (userId, newGroups)=>{
|
|
6
|
-
const user = typeof userId === 'string'
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
export const updateUserPermissions = async (userId, newGroups) => {
|
|
6
|
+
const user = typeof userId === 'string'
|
|
7
|
+
? await prisma.user.findUnique({
|
|
8
|
+
where: {
|
|
9
|
+
id: userId,
|
|
10
|
+
},
|
|
11
|
+
})
|
|
12
|
+
: userId;
|
|
11
13
|
if (!user) {
|
|
12
14
|
return new Err(makeProblem(PROBLEM_NOT_FOUND, 'User not found when trying to update the permissions'));
|
|
13
15
|
}
|
|
14
|
-
const groups = newGroups ? [
|
|
15
|
-
...newGroups
|
|
16
|
-
] : [
|
|
17
|
-
...user.groups
|
|
18
|
-
];
|
|
16
|
+
const groups = newGroups ? [...newGroups] : [...user.groups];
|
|
19
17
|
const ability = await defineAbilityFor({
|
|
20
18
|
...user,
|
|
21
|
-
groups
|
|
19
|
+
groups,
|
|
22
20
|
});
|
|
23
21
|
await prisma.user.update({
|
|
24
22
|
where: {
|
|
25
|
-
id: user.id
|
|
23
|
+
id: user.id,
|
|
26
24
|
},
|
|
27
25
|
data: {
|
|
28
|
-
groups: [
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
permissions: ability.rules
|
|
32
|
-
}
|
|
26
|
+
groups: [...groups],
|
|
27
|
+
permissions: ability.rules,
|
|
28
|
+
},
|
|
33
29
|
});
|
|
34
30
|
user.groups = groups;
|
|
35
31
|
user.permissions = ability.rules;
|
|
36
|
-
return new Ok({
|
|
37
|
-
user,
|
|
38
|
-
ability
|
|
39
|
-
});
|
|
32
|
+
return new Ok({ user, ability });
|
|
40
33
|
};
|
|
@@ -1,37 +1,25 @@
|
|
|
1
1
|
import { subject } from '@casl/ability';
|
|
2
2
|
import { pascalCase } from 'change-case';
|
|
3
|
-
export const detectSubjectType = (subject)=>{
|
|
3
|
+
export const detectSubjectType = (subject) => {
|
|
4
4
|
if (!('object' in subject)) {
|
|
5
5
|
throw new Error(`Trying to get an object from model ${subject} that is not a real model`);
|
|
6
6
|
}
|
|
7
7
|
return pascalCase(subject.object);
|
|
8
8
|
};
|
|
9
9
|
const documents = {
|
|
10
|
-
identification: [
|
|
11
|
-
|
|
12
|
-
'passport',
|
|
13
|
-
'idFront',
|
|
14
|
-
'idBack',
|
|
15
|
-
'drivingLicense'
|
|
16
|
-
],
|
|
17
|
-
drivingHistory: [
|
|
18
|
-
'pdrm',
|
|
19
|
-
'jpj',
|
|
20
|
-
'bgs'
|
|
21
|
-
],
|
|
10
|
+
identification: ['visa', 'passport', 'idFront', 'idBack', 'drivingLicense'],
|
|
11
|
+
drivingHistory: ['pdrm', 'jpj', 'bgs'],
|
|
22
12
|
financial: [
|
|
23
13
|
'experianReport',
|
|
24
14
|
'bankStatement',
|
|
25
15
|
'bankStatement2',
|
|
26
16
|
'bankStatement3',
|
|
27
|
-
'epfStatement'
|
|
17
|
+
'epfStatement',
|
|
28
18
|
],
|
|
29
|
-
offerLetter: [
|
|
30
|
-
'offerLetter'
|
|
31
|
-
]
|
|
19
|
+
offerLetter: ['offerLetter'],
|
|
32
20
|
};
|
|
33
|
-
export const getDocumentsFields = (documentsGroup)=>documents[documentsGroup].map((d)
|
|
34
|
-
export const getPricingFields = ()=>{
|
|
21
|
+
export const getDocumentsFields = (documentsGroup) => documents[documentsGroup].map((d) => `documents.${d}`);
|
|
22
|
+
export const getPricingFields = () => {
|
|
35
23
|
return [
|
|
36
24
|
'pricing',
|
|
37
25
|
'basePrice',
|
|
@@ -39,17 +27,14 @@ export const getPricingFields = ()=>{
|
|
|
39
27
|
'basePricePlan12',
|
|
40
28
|
'basePricePlan24',
|
|
41
29
|
'basePricePlan36',
|
|
42
|
-
'basePricePlan60'
|
|
30
|
+
'basePricePlan60',
|
|
43
31
|
];
|
|
44
32
|
};
|
|
45
33
|
// we are creating a helper function to fake an object
|
|
46
|
-
export const s = (modelName, key = 'id')=>{
|
|
34
|
+
export const s = (modelName, key = 'id') => {
|
|
47
35
|
const objectName = getObjectName(modelName);
|
|
48
|
-
return subject(modelName, {
|
|
49
|
-
object: objectName,
|
|
50
|
-
[key]: 'XXXXXXXXX'
|
|
51
|
-
});
|
|
36
|
+
return subject(modelName, { object: objectName, [key]: 'XXXXXXXXX' });
|
|
52
37
|
};
|
|
53
|
-
const getObjectName = (modelName)=>{
|
|
38
|
+
const getObjectName = (modelName) => {
|
|
54
39
|
return modelName[0].toLowerCase() + modelName.slice(1);
|
|
55
40
|
};
|
package/dist/context.js
CHANGED
|
@@ -2,15 +2,16 @@ import { createContext } from 'react';
|
|
|
2
2
|
const defaultContext = {
|
|
3
3
|
user: undefined,
|
|
4
4
|
isLoadingUser: false,
|
|
5
|
-
signIn: async ()=>Promise.resolve(undefined),
|
|
6
|
-
refresh: async ()=>Promise.resolve(undefined),
|
|
5
|
+
signIn: async () => Promise.resolve(undefined),
|
|
6
|
+
refresh: async () => Promise.resolve(undefined),
|
|
7
7
|
/**
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
*
|
|
9
|
+
* @deprecated There's no need for access token anymore, we use cookies
|
|
10
|
+
*/
|
|
11
|
+
getAccessToken: async () => Promise.resolve(undefined),
|
|
11
12
|
// TODO
|
|
12
13
|
// eslint-disable-next-line @typescript-eslint/no-empty-function -- initial state
|
|
13
|
-
logout: ()=>{},
|
|
14
|
-
status: 'loading'
|
|
14
|
+
logout: () => { },
|
|
15
|
+
status: 'loading',
|
|
15
16
|
};
|
|
16
|
-
export const AuthContext =
|
|
17
|
+
export const AuthContext = createContext(defaultContext);
|
package/dist/default.js
CHANGED
|
@@ -5,29 +5,25 @@ import bcrypt from 'bcryptjs';
|
|
|
5
5
|
import { PROBLEM_INVALID_LOGIN } from '../constants.js';
|
|
6
6
|
import { translations } from '../translations.js';
|
|
7
7
|
import { verifyToken } from './verfiy-token.js';
|
|
8
|
-
export const authenticateUser = async ({ login, password })=>{
|
|
8
|
+
export const authenticateUser = async ({ login, password, }) => {
|
|
9
9
|
const isEmail = login.includes('@');
|
|
10
10
|
// Check if the user exists
|
|
11
11
|
const user = await prisma.user.findFirst({
|
|
12
12
|
where: {
|
|
13
|
-
...isEmail
|
|
14
|
-
email: login.toLowerCase().trim()
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
}
|
|
13
|
+
...(isEmail
|
|
14
|
+
? { email: login.toLowerCase().trim() }
|
|
15
|
+
: { phoneNumber: login.replace(/[\s-]/g, '') }),
|
|
16
|
+
},
|
|
19
17
|
});
|
|
20
18
|
if (!user) {
|
|
21
19
|
return new Err(makeProblem(PROBLEM_INVALID_LOGIN, translations.wrongUsernameOrPassword));
|
|
22
20
|
}
|
|
23
21
|
// Check the user's password
|
|
24
|
-
if (isEmail && !await bcrypt.compare(password, user.password)) {
|
|
22
|
+
if (isEmail && !(await bcrypt.compare(password, user.password))) {
|
|
25
23
|
return new Err(makeProblem(PROBLEM_INVALID_LOGIN, 'The email / password combination is invalid.'));
|
|
26
24
|
}
|
|
27
25
|
if (!isEmail) {
|
|
28
|
-
const tokenResult = await verifyToken(password, {
|
|
29
|
-
scope: 'verify-phone'
|
|
30
|
-
});
|
|
26
|
+
const tokenResult = await verifyToken(password, { scope: 'verify-phone' });
|
|
31
27
|
if (!tokenResult.ok) {
|
|
32
28
|
return new Err(makeProblem(PROBLEM_INVALID_LOGIN, 'The OTP is invalid.'));
|
|
33
29
|
}
|
package/dist/server/cors.js
CHANGED
|
@@ -1,39 +1,28 @@
|
|
|
1
1
|
import { config } from '@driveflux/config/backend';
|
|
2
2
|
import Cors from 'cors';
|
|
3
3
|
const corsOptions = {
|
|
4
|
-
origin: (origin, callback)=>{
|
|
5
|
-
const allowedOrigins = [
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
if (config.appEnv === 'development' || origin && allowedOrigins.includes(origin)) {
|
|
4
|
+
origin: (origin, callback) => {
|
|
5
|
+
const allowedOrigins = [config.appUrl];
|
|
6
|
+
if (config.appEnv === 'development' ||
|
|
7
|
+
(origin && allowedOrigins.includes(origin))) {
|
|
9
8
|
callback(null, true);
|
|
10
|
-
}
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
11
|
callback(new Error('CORS not allowed'));
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
|
-
allowedHeaders: [
|
|
15
|
-
'content-type',
|
|
16
|
-
'x-correlation-id',
|
|
17
|
-
'authorization'
|
|
18
|
-
],
|
|
14
|
+
allowedHeaders: ['content-type', 'x-correlation-id', 'authorization'],
|
|
19
15
|
exposedHeaders: '*',
|
|
20
|
-
methods: [
|
|
21
|
-
|
|
22
|
-
'HEAD',
|
|
23
|
-
'PUT',
|
|
24
|
-
'PATCH',
|
|
25
|
-
'POST',
|
|
26
|
-
'DELETE'
|
|
27
|
-
],
|
|
28
|
-
credentials: true
|
|
16
|
+
methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'],
|
|
17
|
+
credentials: true,
|
|
29
18
|
};
|
|
30
19
|
// Initializing the cors middleware
|
|
31
20
|
// I don't have time to debug cors now, I've got a million things to do.
|
|
32
21
|
// TODO Implement this properly please
|
|
33
22
|
const corsHandler = config.appEnv === 'development' ? Cors(corsOptions) : Cors();
|
|
34
|
-
export const cors = (req, res)=>{
|
|
35
|
-
return new Promise((resolve, reject)=>{
|
|
36
|
-
corsHandler(req, res, (result)=>{
|
|
23
|
+
export const cors = (req, res) => {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
corsHandler(req, res, (result) => {
|
|
37
26
|
if (result instanceof Error) {
|
|
38
27
|
return reject(result);
|
|
39
28
|
}
|