Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 18 additions & 63 deletions api/src/services/block/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
MapBackgroundService,
} from '../map-background/map-background.service';
import { resolveMapTheme } from '../../libs';
import { PlaceCapabilityService } from '../place/place-capability.service';

/** Service for dealing with blocks */
@Service()
Expand All @@ -32,6 +33,7 @@ export class BlockService {
private roleRepository: RoleRepository,
private memberRepository: MemberRepository,
private mapBackgroundService: MapBackgroundService,
private placeCapabilityService: PlaceCapabilityService,
) {}

public async find(blockId: number): Promise<Place> {
Expand Down Expand Up @@ -231,73 +233,26 @@ export class BlockService {
return await this.mapLocationRepository.createAvailableLocation(blockId, location);
}

/**
* Reports whether a member may administer a block.
* @param blockId id of the block
* @param memberId id of the member acting
* @returns true when the member holds the classic owner capability at this block
*/
public async canAdmin(blockId: number, memberId: number): Promise<boolean> {
const roleAssignments = await this.roleAssignmentRepository.getByMemberId(memberId);
const hood = await this.getHood(blockId);
const hoodMapLocation = await this.mapLocationRepository.findPlaceIdMapLocation(hood.id);
const colonyId = hoodMapLocation.parent_place_id;

if (
roleAssignments.find(assignment => {
return (
[
this.roleRepository.roleMap.Admin,
this.roleRepository.roleMap.ColonyRepresentative,
].includes(assignment.role_id) ||
([
this.roleRepository.roleMap.ColonyLeader,
this.roleRepository.roleMap.ColonyDeputy,
].includes(assignment.role_id) &&
assignment.place_id === colonyId) ||
([
this.roleRepository.roleMap.NeighborhoodDeputy,
this.roleRepository.roleMap.NeighborhoodLeader,
].includes(assignment.role_id) &&
assignment.place_id === hood.id) ||
([
this.roleRepository.roleMap.BlockDeputy,
this.roleRepository.roleMap.BlockLeader,
].includes(assignment.role_id) &&
assignment.place_id === blockId)
);
})
) {
return true;
}
return false;
const { canAdmin } = await this.placeCapabilityService.resolve(blockId, memberId);
return canAdmin;
}

/**
* Reports whether a member may change a block's access rights.
* @param blockId id of the block
* @param memberId id of the member acting
* @returns true when the member holds the classic rights capability at this block
*/
public async canManageAccess(blockId: number, memberId: number): Promise<boolean> {
const roleAssignments = await this.roleAssignmentRepository.getByMemberId(memberId);
const hood = await this.getHood(blockId);
const hoodMapLocation = await this.mapLocationRepository.findPlaceIdMapLocation(hood.id);
const colonyId = hoodMapLocation.parent_place_id;

if (
roleAssignments.find(assignment => {
return (
[
this.roleRepository.roleMap.Admin,
this.roleRepository.roleMap.ColonyRepresentative,
].includes(assignment.role_id) ||
([
this.roleRepository.roleMap.ColonyLeader,
this.roleRepository.roleMap.ColonyDeputy,
].includes(assignment.role_id) &&
assignment.place_id === colonyId) ||
([
this.roleRepository.roleMap.NeighborhoodDeputy,
this.roleRepository.roleMap.NeighborhoodLeader,
].includes(assignment.role_id) &&
assignment.place_id === hood.id) ||
([this.roleRepository.roleMap.BlockLeader].includes(assignment.role_id) &&
assignment.place_id === blockId)
);
})
) {
return true;
}
return false;
const { canManageAccess } = await this.placeCapabilityService.resolve(blockId, memberId);
return canManageAccess;
}

private async updateDeputyId(deputy: any): Promise<number> {
Expand Down
55 changes: 18 additions & 37 deletions api/src/services/colony/colony.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
MemberRepository,
} from '../../repositories';
import { Place } from '../../types/models';
import { PlaceCapabilityService } from '../place/place-capability.service';
import * as console from 'console';
import { includes } from 'lodash';

Expand All @@ -18,6 +19,7 @@ export class ColonyService {
private roleAssignmentRepository: RoleAssignmentRepository,
private roleRepository: RoleRepository,
private memberRepository: MemberRepository,
private placeCapabilityService: PlaceCapabilityService,
) { }

public async find(colonyId: number): Promise<Place> {
Expand Down Expand Up @@ -138,47 +140,26 @@ export class ColonyService {
});
}

/**
* Reports whether a member may administer a colony.
* @param colonyId id of the colony
* @param memberId id of the member acting
* @returns true when the member holds the classic owner capability at this colony
*/
public async canAdmin(colonyId: number, memberId: number): Promise<boolean> {
const roleAssignments = await this.roleAssignmentRepository.getByMemberId(memberId);

if (
roleAssignments.find(assignment => {
return (
[
this.roleRepository.roleMap.Admin,
this.roleRepository.roleMap.ColonyRepresentative,
].includes(assignment.role_id) ||
([
this.roleRepository.roleMap.ColonyLeader,
this.roleRepository.roleMap.ColonyDeputy,
].includes(assignment.role_id) &&
assignment.place_id === colonyId)
);
})
) {
return true;
}
else return false;
const { canAdmin } = await this.placeCapabilityService.resolve(colonyId, memberId);
return canAdmin;
}

/**
* Reports whether a member may change a colony's access rights.
* @param colonyId id of the colony
* @param memberId id of the member acting
* @returns true when the member holds the classic rights capability at this colony
*/
public async canManageAccess(colonyId: number, memberId: number): Promise<boolean> {
const roleAssignments = await this.roleAssignmentRepository.getByMemberId(memberId);

if (
roleAssignments.find(assignment => {
return (
[
this.roleRepository.roleMap.Admin,
this.roleRepository.roleMap.ColonyRepresentative,
].includes(assignment.role_id) ||
([this.roleRepository.roleMap.ColonyLeader].includes(assignment.role_id) &&
assignment.place_id === colonyId)
);
})
) {
return true;
}
return false;
const { canManageAccess } = await this.placeCapabilityService.resolve(colonyId, memberId);
return canManageAccess;
}

private async updateDeputyId(deputy: any): Promise<number> {
Expand Down
67 changes: 18 additions & 49 deletions api/src/services/hood/hood.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
MapBackgroundService,
} from '../map-background/map-background.service';
import { resolveMapTheme } from '../../libs';
import { PlaceCapabilityService } from '../place/place-capability.service';

/** Service for dealing with blocks */
@Service()
Expand All @@ -30,6 +31,7 @@ export class HoodService {
private roleRepository: RoleRepository,
private memberRepository: MemberRepository,
private mapBackgroundService: MapBackgroundService,
private placeCapabilityService: PlaceCapabilityService,
) {}

public async find(hoodId: number): Promise<Place> {
Expand Down Expand Up @@ -207,59 +209,26 @@ export class HoodService {
return { status: 'success', selectedIndex: normalizedIndex };
}

/**
* Reports whether a member may administer a neighborhood.
* @param hoodId id of the neighborhood
* @param memberId id of the member acting
* @returns true when the member holds the classic owner capability at this neighborhood
*/
public async canAdmin(hoodId: number, memberId: number): Promise<boolean> {
const roleAssignments = await this.roleAssignmentRepository.getByMemberId(memberId);
const colony = await this.getColony(hoodId);

if (
roleAssignments.find(assignment => {
return (
[
this.roleRepository.roleMap.Admin,
this.roleRepository.roleMap.ColonyRepresentative,
].includes(assignment.role_id) ||
([
this.roleRepository.roleMap.ColonyLeader,
this.roleRepository.roleMap.ColonyDeputy,
].includes(assignment.role_id) &&
assignment.place_id === colony.id) ||
([
this.roleRepository.roleMap.NeighborhoodDeputy,
this.roleRepository.roleMap.NeighborhoodLeader,
].includes(assignment.role_id) &&
assignment.place_id === hoodId)
);
})
) {
return true;
}
return false;
const { canAdmin } = await this.placeCapabilityService.resolve(hoodId, memberId);
return canAdmin;
}

/**
* Reports whether a member may change a neighborhood's access rights.
* @param hoodId id of the neighborhood
* @param memberId id of the member acting
* @returns true when the member holds the classic rights capability at this neighborhood
*/
public async canManageAccess(hoodId: number, memberId: number): Promise<boolean> {
const roleAssignments = await this.roleAssignmentRepository.getByMemberId(memberId);
const colony = await this.getColony(hoodId);

if (
roleAssignments.find(assignment => {
return (
[
this.roleRepository.roleMap.Admin,
this.roleRepository.roleMap.ColonyRepresentative,
].includes(assignment.role_id) ||
([
this.roleRepository.roleMap.ColonyLeader,
this.roleRepository.roleMap.ColonyDeputy,
].includes(assignment.role_id) &&
assignment.place_id === colony.id) ||
([this.roleRepository.roleMap.NeighborhoodLeader].includes(assignment.role_id) &&
assignment.place_id === hoodId)
);
})
) {
return true;
}
return false;
const { canManageAccess } = await this.placeCapabilityService.resolve(hoodId, memberId);
return canManageAccess;
}

private async updateDeputyId(deputy: any): Promise<number> {
Expand Down
1 change: 1 addition & 0 deletions api/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export * from './object/object.service';
export * from './object-instance/object-instance.service';
export * from './role/role.service';
export * from './role-assignment/role-assignment.service';
export * from './place/place-capability.service';
export * from './place/place.service';
export * from './wallet/wallet.service';
export * from './messageboard/messageboard.service';
Expand Down
Loading