Skip to content

Commit 56f1777

Browse files
committed
All site access and bug fixes
1 parent 3cdf4b6 commit 56f1777

File tree

13 files changed

+160
-63
lines changed

13 files changed

+160
-63
lines changed

db/scripts/June_updates.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
INSERT INTO
2+
SECURITY.SiteAccessType
3+
VALUES
4+
(4, 'All Sites');
5+
6+
CREATE TABLE [dbo].[PlaceTheme](
7+
[Id] [int] IDENTITY(1, 1) NOT NULL,
8+
[Category] [nvarchar](100) NOT NULL,
9+
[Type] [nvarchar](100) NOT NULL,
10+
CONSTRAINT [PK_PlaceTheme] PRIMARY KEY CLUSTERED ([Id] ASC) WITH (
11+
PAD_INDEX = OFF,
12+
STATISTICS_NORECOMPUTE = OFF,
13+
IGNORE_DUP_KEY = OFF,
14+
ALLOW_ROW_LOCKS = ON,
15+
ALLOW_PAGE_LOCKS = ON
16+
) ON [PRIMARY]
17+
) ON [PRIMARY]
18+
GO

src/api/models/site-access-type.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ export class SiteAccesType {
1818
static get FIRST_NATION(): number {
1919
return 3;
2020
}
21+
static get ALL_SITES(): number {
22+
return 4;
23+
}
2124
}

src/api/models/user.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,11 @@ export class User {
119119
.filter((a) => a.accessTypeId == SiteAccesType.FIRST_NATION)
120120
.map((a) => toInteger(a.accessText));
121121
}
122+
123+
get canAccessAllSites(): boolean {
124+
return (
125+
this.siteAccess.filter((a) => a.accessTypeId == SiteAccesType.ALL_SITES)
126+
.length > 0
127+
);
128+
}
122129
}

src/api/policies/base-policy-scope.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export abstract class BasePolicyScope {
1919
return this.scope.whereRaw('(1=0)');
2020
}
2121

22+
get unlimitedAccessScope(): Knex.QueryBuilder {
23+
return this.scope.whereRaw('(1=1)');
24+
}
25+
2226
get scope(): Knex.QueryBuilder {
2327
return this._scope.clone();
2428
}

src/api/policies/place-policy-scope.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { isEmpty, intersection } from 'lodash';
2-
import { Knex } from 'knex';
32

43
import { BasePolicyScope } from '.';
5-
import { User, UserRoles } from '../models';
4+
import { UserRoles } from '../models';
65

76
export class PlacePolicyScope extends BasePolicyScope {
87
resolve() {
@@ -29,7 +28,9 @@ export class PlacePolicyScope extends BasePolicyScope {
2928
return this.emptyScope;
3029
}
3130

32-
let clauses = [];
31+
if (this.user.canAccessAllSites) return this.unlimitedAccessScope;
32+
33+
const clauses = [];
3334
if (!isEmpty(this.user.permittedMapSheets)) {
3435
const permittedMapSheets = this.user.permittedMapSheets.join(', ');
3536
clauses.push(`NTSMapSheet IN ('${permittedMapSheets}')`);

src/api/policies/place-policy.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export class PlacePolicy extends BasePolicy<Place> {
1010

1111
show() {
1212
if (this.user.roleList.includes(UserRoles.ADMINISTRATOR)) return true;
13+
if (this.user.canAccessAllSites) return true;
14+
1315
if (
1416
isEmpty(
1517
intersection(this.user.roleList, [

src/api/services/place-service.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -567,26 +567,31 @@ export class PlaceService {
567567
}
568568

569569
if (user.site_access) {
570-
let mapSheets = user.site_access
570+
const mapSheets = user.site_access
571571
.filter((a) => a.access_type_id == 1)
572572
.map((a) => a.access_text);
573-
let communities = user.site_access
573+
const communities = user.site_access
574574
.filter((a) => a.access_type_id == 2)
575575
.map((a) => a.access_text);
576-
let firstNations = user.site_access
576+
const firstNations = user.site_access
577577
.filter((a) => a.access_type_id == 3)
578578
.map((a) => a.access_text);
579+
const allAccess = user.site_access.find((a) => a.access_type_id == 4);
579580

580581
let scope = '(1=0';
581582

582-
if (mapSheets.length > 0)
583-
scope += ` OR NTSMapSheet IN ('${mapSheets.join("','")}')`;
584-
if (communities.length > 0)
585-
scope += ` OR CommunityId IN (${communities.join(',')})`;
586-
if (firstNations.length > 0)
587-
scope += ` OR [FirstNationAssociation].[FirstNationId] IN (${firstNations.join(
588-
','
589-
)})`;
583+
if (allAccess) {
584+
scope += ' OR 1=1';
585+
} else {
586+
if (mapSheets.length > 0)
587+
scope += ` OR NTSMapSheet IN ('${mapSheets.join("','")}')`;
588+
if (communities.length > 0)
589+
scope += ` OR CommunityId IN (${communities.join(',')})`;
590+
if (firstNations.length > 0)
591+
scope += ` OR [FirstNationAssociation].[FirstNationId] IN (${firstNations.join(
592+
','
593+
)})`;
594+
}
590595

591596
scope += ')';
592597

src/api/services/user-service.ts

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,23 @@ export class UserService {
2727
}
2828

2929
async getByEmail(email: string): Promise<User | undefined> {
30-
let user = await this.knex("Security.User").where({ email }).first();
30+
let user = await this.knex('Security.User').where({ email }).first();
3131

32-
if (user)
33-
return this.loadDetails(user);
32+
if (user) return this.loadDetails(user);
3433

3534
return undefined;
3635
}
3736

3837
async getById(id: number): Promise<User | undefined> {
39-
let user = await this.knex("Security.User").where({ id }).first();
38+
let user = await this.knex('Security.User').where({ id }).first();
4039

41-
if (user)
42-
return this.loadDetails(user);
40+
if (user) return this.loadDetails(user);
4341

4442
return undefined;
4543
}
4644

4745
async getAll(): Promise<User[]> {
48-
let list = await this.knex("Security.User");
46+
let list = await this.knex('Security.User');
4947

5048
for (let user of list) {
5149
await this.loadDetails(user);
@@ -68,8 +66,8 @@ export class UserService {
6866
return [];
6967
});
7068

71-
let allCommunities = await this.knex("Community");
72-
let allFirstNations = await this.knex("FirstNation")
69+
let allCommunities = await this.knex('Community');
70+
let allFirstNations = await this.knex('FirstNation');
7371

7472
for (let access of user.site_access) {
7573
switch (access.access_type_id) {
@@ -80,16 +78,22 @@ export class UserService {
8078
case SiteAccesType.COMMUNITY:
8179
access.access_type_name = 'Community';
8280
access.access_text = parseInt(access.access_text.toString());
83-
let cm = allCommunities.filter((c: any) => c.Id == access.access_text)
84-
if (cm.length > 0)
85-
access.access_text_name = cm[0].Name;
81+
let cm = allCommunities.filter(
82+
(c: any) => c.Id == access.access_text
83+
);
84+
if (cm.length > 0) access.access_text_name = cm[0].Name;
8685
break;
8786
case SiteAccesType.FIRST_NATION:
8887
access.access_type_name = 'First Nation';
8988
access.access_text = parseInt(access.access_text.toString());
90-
let fn = allFirstNations.filter((c: any) => c.Id == access.access_text)
91-
if (fn.length > 0)
92-
access.access_text_name = fn[0].Description;
89+
let fn = allFirstNations.filter(
90+
(c: any) => c.Id == access.access_text
91+
);
92+
if (fn.length > 0) access.access_text_name = fn[0].Description;
93+
break;
94+
case SiteAccesType.ALL_SITES:
95+
access.access_type_name = 'All Sites';
96+
access.access_text = '';
9397
break;
9498
}
9599
}
@@ -98,34 +102,46 @@ export class UserService {
98102
}
99103

100104
async update(id: any, value: any) {
101-
if (value.role_list)
102-
value.roles = value.role_list.join(", ");
103-
else
104-
value.roles = "";
105+
if (value.role_list) value.roles = value.role_list.join(', ');
106+
else value.roles = '';
105107

106108
delete value.role_list;
107-
await this.knex("Security.User").where({ id }).update(value);
109+
await this.knex('Security.User').where({ id }).update(value);
108110
}
109111

110-
async create(email: string, first_name: string, last_name: string): Promise<User[]> {
112+
async create(
113+
email: string,
114+
first_name: string,
115+
last_name: string
116+
): Promise<User[]> {
111117
email = email.toLocaleLowerCase();
112-
console.log("-- Creating User account for " + email);
113-
return this.knex("Security.User").insert({ email, first_name, last_name, last_login_date: new Date(), status: "Pending" }).returning("*")
118+
console.log('-- Creating User account for ' + email);
119+
return this.knex('Security.User')
120+
.insert({
121+
email,
122+
first_name,
123+
last_name,
124+
last_login_date: new Date(),
125+
status: 'Pending',
126+
})
127+
.returning('*');
114128
}
115129

116130
async updateLoginDate(user: User): Promise<any> {
117-
return this.knex("Security.User").where({ id: user.id }).update({ last_login_date: new Date() });
131+
return this.knex('Security.User')
132+
.where({ id: user.id })
133+
.update({ last_login_date: new Date() });
118134
}
119135

120136
createAccess(value: any): Promise<any> {
121-
return this.knex("Security.UserSiteAccess").insert(value);
137+
return this.knex('Security.UserSiteAccess').insert(value);
122138
}
123139

124140
updateAccess(id: any, value: any): Promise<any> {
125-
return this.knex("Security.UserSiteAccess").where({ id }).update(value);
141+
return this.knex('Security.UserSiteAccess').where({ id }).update(value);
126142
}
127143

128144
deleteAccess(id: any): Promise<any> {
129-
return this.knex("Security.UserSiteAccess").where({ id }).delete()
145+
return this.knex('Security.UserSiteAccess').where({ id }).delete();
130146
}
131147
}

0 commit comments

Comments
 (0)