-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working version for /Groups and /Groups/:id
- Loading branch information
Showing
6 changed files
with
311 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { HomeDBManager } from "app/gen-server/lib/homedb/HomeDBManager"; | ||
import { ApiError } from "app/common/ApiError"; | ||
import { LogMethods } from "app/server/lib/LogMethods"; | ||
import { RequestContext } from 'app/server/lib/scim/v2/ScimTypes'; | ||
|
||
import SCIMMY from "scimmy"; | ||
|
||
export class BaseController { | ||
protected static getIdFromResource(resource: any) { | ||
const id = parseInt(resource.id, 10); | ||
if (Number.isNaN(id)) { | ||
throw new SCIMMY.Types.Error(400, 'invalidValue', 'Invalid passed group ID'); | ||
} | ||
return id; | ||
} | ||
|
||
protected logger = new LogMethods(this.constructor.name, () => ({})); | ||
|
||
constructor( | ||
protected dbManager: HomeDBManager, | ||
protected checkAccess: (context: RequestContext) => void | ||
) {} | ||
|
||
/** | ||
* Runs the passed callback and handles any errors that might occur. | ||
* Also checks if the user has access to the operation. | ||
* Any public method of this class should be run through this method. | ||
* | ||
* @param context The request context to check access for the user | ||
* @param cb The callback to run | ||
*/ | ||
protected async runAndHandleErrors<T>(context: RequestContext, cb: () => Promise<T>): Promise<T> { | ||
try { | ||
this.checkAccess(context); | ||
return await cb(); | ||
} catch (err) { | ||
if (err instanceof ApiError) { | ||
this.logger.error(null, ' ApiError: ', err.status, err.message); | ||
if (err.status === 409) { | ||
throw new SCIMMY.Types.Error(err.status, 'uniqueness', err.message); | ||
} | ||
throw new SCIMMY.Types.Error(err.status, null!, err.message); | ||
} | ||
if (err instanceof SCIMMY.Types.Error) { | ||
this.logger.error(null, ' SCIMMY.Types.Error: ', err.message); | ||
throw err; | ||
} | ||
// By default, return a 500 error | ||
this.logger.error(null, ' Error: ', err.message); | ||
throw new SCIMMY.Types.Error(500, null!, err.message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Group } from 'app/gen-server/entity/Group'; | ||
import { HomeDBManager } from 'app/gen-server/lib/homedb/HomeDBManager'; | ||
import { BaseController } from 'app/server/lib/scim/v2/BaseController'; | ||
import { RequestContext } from 'app/server/lib/scim/v2/ScimTypes'; | ||
import { toSCIMMYGroup } from 'app/server/lib/scim/v2/ScimUtils'; | ||
|
||
import SCIMMY from 'scimmy'; | ||
|
||
class ScimGroupController extends BaseController { | ||
/** | ||
* Gets a single group with the passed ID. | ||
* | ||
* @param resource The SCIMMY group resource performing the operation | ||
* @param context The request context | ||
*/ | ||
public async getSingleGroup(resource: any, context: RequestContext) { | ||
return this.runAndHandleErrors(context, async () => { | ||
const id = ScimGroupController.getIdFromResource(resource); | ||
const group = await this.dbManager.getGroupWithMembersById(id); | ||
if (!group || group.type !== Group.RESOURCE_USERS_TYPE) { | ||
throw new SCIMMY.Types.Error(404, null!, `Group with ID ${id} not found`); | ||
} | ||
return toSCIMMYGroup(group); | ||
}); | ||
} | ||
|
||
/** | ||
* Gets all groups. | ||
* @param resource The SCIMMY group resource performing the operation | ||
* @param context The request context | ||
* @returns All groups | ||
*/ | ||
public async getGroups(resource: any, context: RequestContext) { | ||
return this.runAndHandleErrors(context, async () => { | ||
const { filter } = resource; | ||
const scimmyGroup = (await this.dbManager.getGroupsWithMembersByType(Group.RESOURCE_USERS_TYPE)) | ||
.map(group => toSCIMMYGroup(group)); | ||
return filter ? filter.match(scimmyGroup) : scimmyGroup; | ||
}); | ||
} | ||
} | ||
|
||
export const getScimGroupConfig = ( | ||
dbManager: HomeDBManager, checkAccess: (context: RequestContext) => void | ||
) => { | ||
const controller = new ScimGroupController(dbManager, checkAccess); | ||
|
||
return { | ||
egress: async (resource: any, context: RequestContext) => { | ||
if (resource.id) { | ||
return await controller.getSingleGroup(resource, context); | ||
} | ||
return await controller.getGroups(resource, context); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.