mirror of
https://github.com/OpenSignLabs/OpenSign.git
synced 2026-08-21 15:12:34 +02:00
feat: add share-with opt in templates as well as restrict non-team user
This commit is contained in:
@@ -16,22 +16,45 @@ export default async function GetTemplate(request) {
|
||||
// console.log("userId ",userId)
|
||||
if (templateId && userId) {
|
||||
try {
|
||||
const template = new Parse.Query('contracts_Template');
|
||||
let template = new Parse.Query('contracts_Template');
|
||||
template.equalTo('objectId', templateId);
|
||||
template.include('ExtUserPtr');
|
||||
template.include('Signers');
|
||||
template.include('CreateBy');
|
||||
template.include('CreatedBy');
|
||||
|
||||
const extUserQuery = new Parse.Query('contracts_Users');
|
||||
extUserQuery.equalTo('Email', userRes.data.email);
|
||||
extUserQuery.include('DepartmentIds');
|
||||
const extUser = await extUserQuery.first({ useMasterKey: true });
|
||||
if (extUser) {
|
||||
const _extUser = JSON.parse(JSON.stringify(extUser));
|
||||
if (_extUser?.DepartmentIds && _extUser.DepartmentIds?.length > 0) {
|
||||
let departmentArr = [];
|
||||
_extUser?.DepartmentIds?.forEach(
|
||||
x => (departmentArr = [...departmentArr, ...x.Ancestors])
|
||||
);
|
||||
// Create the first query
|
||||
const sharedWithQuery = new Parse.Query('contracts_Template');
|
||||
sharedWithQuery.containedIn('SharedWith', departmentArr);
|
||||
|
||||
// Create the second query
|
||||
const createdByQuery = new Parse.Query('contracts_Template');
|
||||
createdByQuery.equalTo('ExtUserPtr', {
|
||||
__type: 'Pointer',
|
||||
className: 'contracts_Users',
|
||||
objectId: extUser.id,
|
||||
});
|
||||
template = Parse.Query.or(sharedWithQuery, createdByQuery);
|
||||
template.equalTo('objectId', templateId);
|
||||
template.include('ExtUserPtr');
|
||||
template.include('Signers');
|
||||
template.include('CreatedBy');
|
||||
}
|
||||
}
|
||||
const res = await template.first({ useMasterKey: true });
|
||||
// console.log("res ", res)
|
||||
if (res) {
|
||||
// console.log("res ",res)
|
||||
const acl = res.getACL();
|
||||
// console.log("acl", acl.getReadAccess(userId))
|
||||
if (acl && acl.getReadAccess(userId)) {
|
||||
return res;
|
||||
} else {
|
||||
return { error: "You don't have access of this document!" };
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
return { error: "You don't have access of this document!" };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
async function addDepartmentAndOrg(extUser) {
|
||||
try {
|
||||
const orgCls = new Parse.Object('contracts_Organizations');
|
||||
orgCls.set('Name', extUser.Company);
|
||||
orgCls.set('IsActive', true);
|
||||
const orgRes = await orgCls.save(null, { useMasterKey: true });
|
||||
const departmentCls = new Parse.Object('contracts_Departments');
|
||||
departmentCls.set('Name', 'All User');
|
||||
departmentCls.set('OrganizationId', {
|
||||
__type: 'Pointer',
|
||||
className: 'contracts_Organizations',
|
||||
objectId: orgRes.id,
|
||||
});
|
||||
departmentCls.set('IsActive', true);
|
||||
const departmentRes = await departmentCls.save(null, { useMasterKey: true });
|
||||
const updateUser = new Parse.Object('contracts_Users');
|
||||
updateUser.id = extUser.objectId;
|
||||
updateUser.set('UserRole', 'contracts_Admin');
|
||||
updateUser.set('OrganizationId', {
|
||||
__type: 'Pointer',
|
||||
className: 'contracts_Organizations',
|
||||
objectId: orgRes.id,
|
||||
});
|
||||
updateUser.set('DepartmentIds', [
|
||||
{
|
||||
__type: 'Pointer',
|
||||
className: 'contracts_Departments',
|
||||
objectId: departmentRes.id,
|
||||
},
|
||||
]);
|
||||
const extUserRes = await updateUser.save(null, { useMasterKey: true });
|
||||
} catch (err) {
|
||||
console.log('err in add department, role, org', err);
|
||||
}
|
||||
}
|
||||
|
||||
export default async function SubscriptionAftersave(request) {
|
||||
const oldObj = request.original;
|
||||
if (!oldObj) {
|
||||
try {
|
||||
const subscription = new Parse.Query('contracts_Subscriptions');
|
||||
subscription.include('CreatedBy');
|
||||
const res = await subscription.get(request.object.id, { useMasterKey: true });
|
||||
const _res = JSON.parse(JSON.stringify(res));
|
||||
const user = _res.CreatedBy?.email;
|
||||
if (user) {
|
||||
const extUserQuery = new Parse.Query('contracts_Users');
|
||||
extUserQuery.equalTo('Email', user);
|
||||
const extUserRes = await extUserQuery.first({ useMasterKey: true });
|
||||
if (extUserRes) {
|
||||
const extUser = JSON.parse(JSON.stringify(extUserRes));
|
||||
if (extUser?.UserRole !== 'contracts_Admin') {
|
||||
await addDepartmentAndOrg(extUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('Err in subscriptionaftersave', err);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
const subscription = new Parse.Query('contracts_Subscriptions');
|
||||
subscription.include('CreatedBy');
|
||||
const res = await subscription.get(request.object.id, { useMasterKey: true });
|
||||
const _res = JSON.parse(JSON.stringify(res));
|
||||
const user = _res.CreatedBy?.email;
|
||||
if (user) {
|
||||
const extUserQuery = new Parse.Query('contracts_Users');
|
||||
extUserQuery.equalTo('Email', user);
|
||||
const extUserRes = await extUserQuery.first({ useMasterKey: true });
|
||||
if (extUserRes) {
|
||||
const extUser = JSON.parse(JSON.stringify(extUserRes));
|
||||
if (extUser?.UserRole !== 'contracts_Admin') {
|
||||
await addDepartmentAndOrg(extUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('Err in subscriptionaftersave', err);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,10 +42,10 @@ export default async function getReport(request) {
|
||||
$or: [
|
||||
{ SharedWith: { $in: departmentArr } },
|
||||
{
|
||||
CreatedBy: {
|
||||
ExtUserPtr: {
|
||||
__type: 'Pointer',
|
||||
className: '_User',
|
||||
objectId: userId,
|
||||
className: 'contracts_Users',
|
||||
objectId: extUser.id,
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
@@ -82,7 +82,7 @@ export default async function usersignup(request) {
|
||||
if (userDetails?.phone) {
|
||||
partnerQuery.set('ContactNumber', userDetails.phone);
|
||||
}
|
||||
partnerQuery.set('TenantName', userDetails.name);
|
||||
partnerQuery.set('TenantName', userDetails.company);
|
||||
partnerQuery.set('EmailAddress', userDetails.email);
|
||||
partnerQuery.set('IsActive', true);
|
||||
partnerQuery.set('CreatedBy', {
|
||||
|
||||
Reference in New Issue
Block a user