Skip to content

Commit

Permalink
Merge pull request #13 from unipoll/dev
Browse files Browse the repository at this point in the history
Updated policy requests and functionality
  • Loading branch information
mike-pisman authored Oct 23, 2023
2 parents 1804896 + de4be2f commit 078612f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,15 @@ export class DialogSetPolicyComponent {
let request_method = null;

let request_data = {
policy_id: this.policy.id,
permissions: this.permissions
};

console.log("Resource Type", this.data.resource.type);
if (this.data.resource.type == 'workspace') {
console.log("request_data", request_data);
request_method = this._apiService.setWorkspacePolicy(this.data.resource.id, request_data);
request_method = this._apiService.updateWorkspacePolicy(this.data.resource.id, this.policy.id, request_data);
} else if (this.data.resource.type == 'group') {
request_method = this._apiService.setGroupPolicy(this.data.resource.id, request_data);
request_method = this._apiService.updateGroupPolicy(this.policy.id, this.policy.id, request_data);
}


Expand Down
4 changes: 2 additions & 2 deletions src/app/components/group/group.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export class GroupComponent {
this.group = await lastValueFrom(this.apiService.getGroup(this.group_id, true, true));
this.workspace = this.group.workspace;

await lastValueFrom(this.apiService.getGroupPolicy(this.group.id)).then((response: any) => {
this.authService.setPermissions(response.permissions);
await lastValueFrom(this.apiService.getGroupPolicies(this.group.id, this.authService.getAccount()?.id)).then((response: any) => {
this.authService.setPermissions(response.policies[0].permissions);
})
}

Expand Down
18 changes: 7 additions & 11 deletions src/app/components/policy-list/policy-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,19 @@ export class PolicyListComponent {

this.policyList ? this.makeTable(this.policyList) : this.updatePolicyList();

if (this.group)
this.can_set_policies = this.authService.isAllowed('set_group_policy');
else if (this.workspace)
this.can_set_policies = this.authService.isAllowed('set_workspace_policy');

this.can_set_policies = this.authService.isAllowed('update_policies');
}

updatePolicyList() {
let requst;
let request;
if (this.group)
requst = this.apiService.getAllGroupsPolicies(this.group.id);
request = this.apiService.getGroupPolicies(this.group.id);
else if (this.workspace)
requst = this.apiService.getAllWorkspacesPolicies(this.workspace.id);
request = this.apiService.getWorkspacePolicies(this.workspace.id);
else
return;

requst.pipe(
request.pipe(
tap((data) => (
this.makeTable(data.policies)
))
Expand All @@ -89,10 +85,10 @@ export class PolicyListComponent {

makeFullName(policies: Array<PolicyModel>) {
policies.forEach(policy => {
if (policy.policy_holder_type == 'account') {
if (policy.policy_holder_type == 'Member') {
let policy_holder = policy.policy_holder as MemberModel;
policy.name = policy_holder.first_name + ' ' + policy_holder.last_name;
} else if (policy.policy_holder_type == 'group') {
} else if (policy.policy_holder_type == 'Group') {
let policy_holder = policy.policy_holder as GroupModel;
policy.name = policy_holder.name;
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/workspace/workspace.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export class WorkspaceComponent {
if (this.workspace_id) {
this.workspace = await lastValueFrom(this.apiService.getWorkspace(this.workspace_id, true, true, true, true));

await lastValueFrom(this.apiService.getWorkspacePolicy(this.workspace.id)).then((response: any) => {
this.authService.setPermissions(response.permissions);
await lastValueFrom(this.apiService.getWorkspacePolicies(this.workspace.id, this.authService.getAccount()?.id)).then((response: any) => {
this.authService.setPermissions(response.policies[0].permissions);
});
}
else {
Expand Down
49 changes: 24 additions & 25 deletions src/app/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { WorkspaceModel } from '../models/workspace.model';
import { WorkspaceListModel } from '../models/workspace.model';
import { MemberListModel } from '../models/member.model';
import { GroupListModel, GroupModel } from '../models/group.model';
import { AccountListModel } from '../models/account.model';
import { AccountListModel, AccountModel } from '../models/account.model';
import { Permissions, PolicyListModel, PolicyModel } from '../models/policy.model';
import { NewPollRequestBody, PollModel, PollListModel } from '../models/poll.model';
import { SettingsService } from './settings.service';
Expand Down Expand Up @@ -40,6 +40,20 @@ export class ApiService {
}, { observe: 'response' });
}


// Accounts

// Get list of accounts
getAllAccounts(): Observable<AccountListModel> {
return this.http.get<AccountListModel>(this.settings.apiUrl + '/accounts');
}

// Get user account
getUserAccount(): Observable<AccountModel> {
return this.http.get<AccountModel>(this.settings.apiUrl + '/accounts/me');
}


// Workspaces

// Get list of workspaces
Expand Down Expand Up @@ -97,19 +111,14 @@ export class ApiService {
}

// Get all policies
getAllWorkspacesPolicies(workspace_id: string): Observable<PolicyListModel> {
return this.http.get<PolicyListModel>(this.settings.apiUrl + '/workspaces/' + workspace_id + '/policies');
}

// Get workspace policy for specific account, or current user if account_id was not provided
getWorkspacePolicy(workspace_id: string, account_id?: string): Observable<PolicyModel> {
const options = account_id ? { params: { account_id: account_id } } : {};
return this.http.get<PolicyModel>(this.settings.apiUrl + '/workspaces/' + workspace_id + '/policy', options);
getWorkspacePolicies(workspace_id: string, account_id?: string): Observable<PolicyListModel> {
const options = account_id ? { params: { account_id: account_id } } : {};
return this.http.get<PolicyListModel>(this.settings.apiUrl + '/workspaces/' + workspace_id + '/policies', options);
}

// Update workspace policy
setWorkspacePolicy(workspace_id: string, data: any) {
return this.http.put(this.settings.apiUrl + '/workspaces/' + workspace_id + '/policy', data);
updateWorkspacePolicy(workspace_id: string, policy_id: string, data: any) {
return this.http.put(this.settings.apiUrl + '/workspaces/' + workspace_id + '/policies/' + policy_id, data);
}

getWorkspacePermissions(): Observable<Permissions> {
Expand Down Expand Up @@ -170,30 +179,20 @@ export class ApiService {
return this.http.delete(this.settings.apiUrl + '/groups/' + group_id + '/members/' + member_id);
}

// Get all group policies
getAllGroupsPolicies(group_id: string): Observable<PolicyListModel> {
return this.http.get<PolicyListModel>(this.settings.apiUrl + '/groups/' + group_id + '/policies');
}

// Get group policy for specific account, or current user if account_id was not provided
getGroupPolicy(group_id: string, account_id?: string): Observable<PolicyModel> {
getGroupPolicies(group_id: string, account_id?: string): Observable<PolicyListModel> {
const options = account_id ? { params: { account_id: account_id } } : {};
return this.http.get<PolicyModel>(this.settings.apiUrl + '/groups/' + group_id + '/policy', options);
return this.http.get<PolicyListModel>(this.settings.apiUrl + '/groups/' + group_id + '/policies', options);
}

setGroupPolicy(group_id: string, data: any) {
return this.http.put(this.settings.apiUrl + '/groups/' + group_id + '/policy', data);
updateGroupPolicy(group_id: string, policy_id: string, data: any) {
return this.http.put(this.settings.apiUrl + '/groups/' + group_id + '/policies/' + policy_id, data);
}

getGroupPermissions(): Observable<Permissions> {
return this.http.get<Permissions>(this.settings.apiUrl + '/groups/permissions');
}

// Accounts
getAllAccounts(): Observable<AccountListModel> {
return this.http.get<AccountListModel>(this.settings.apiUrl + '/accounts');
}


// Polls

Expand Down
15 changes: 15 additions & 0 deletions src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BehaviorSubject } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { ApiService } from './api.service';
import { SnackBarService } from './snackbar.service';
import { AccountModel } from '../models/account.model';

@Injectable({ providedIn: 'root' })
export class AuthService {
Expand All @@ -12,9 +13,14 @@ export class AuthService {
private _permissions$ = new BehaviorSubject<string[]>([]);
permissions$ = this._permissions$.asObservable();

private _account$ = new BehaviorSubject<AccountModel | null>(null);
account$ = this._account$.asObservable();

constructor(private apiService: ApiService, private snackBarService: SnackBarService) {
const token = localStorage.getItem('access_token');
const account = localStorage.getItem('userAccount');
this._isLoggedIn$.next(!!token);
this._account$.next(account ? JSON.parse(account) : null);
}

setPermissions(permissions: string[]) {
Expand All @@ -26,6 +32,10 @@ export class AuthService {
return this._permissions$.value;
}

getAccount() {
return this._account$.value;
}

isAllowed(permission: string) {
return this._permissions$.value.includes(permission);
}
Expand Down Expand Up @@ -56,6 +66,11 @@ export class AuthService {
tap((response: any) => {
this._isLoggedIn$.next(true);
localStorage.setItem('access_token', response.body.access_token);

this.apiService.getUserAccount().subscribe((account: AccountModel) => {
this._account$.next(account);
localStorage.setItem('userAccount', JSON.stringify(account));
});
})
);
}
Expand Down

0 comments on commit 078612f

Please sign in to comment.