Skip to content

Commit

Permalink
fix: authorisation pour les opcos (#808)
Browse files Browse the repository at this point in the history
* fix: authorisation pour les opcos

* fix: tests
  • Loading branch information
remy-auricoste authored Nov 13, 2023
1 parent a0fb6d8 commit 9ff2417
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion server/src/http/routes/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default (server: Server) => {
"/user/opco",
{
schema: zRoutes.get["/user/opco"],
// onRequest: [server.auth(zRoutes.get["/user/opco"])],
onRequest: [server.auth(zRoutes.get["/user/opco"])],
},
async (req, res) => {
const { opco } = req.query
Expand Down
2 changes: 1 addition & 1 deletion server/src/security/authorisationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ function canAccessUser<S extends Pick<IRouteSchema, "method" | "path"> & WithSec
case "CFA":
return resource._id.toString() === user._id.toString()
case "OPCO":
return resource.type === "OPCO" && resource.scope === user.opco
return (resource.type === "OPCO" && resource._id === user._id) || (resource.type === "ENTREPRISE" && resource.opco === user.scope)
default:
assertUnreachable(user.type)
}
Expand Down
23 changes: 22 additions & 1 deletion server/tests/unit/security/authorisationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,27 @@ describe("authorisationService", () => {
).resolves.toBe(undefined)
})
})
describe.each<[Permission]>([["user:manage"]])("I have %s permission", (permission) => {
it("on user recruiter from my Opco", async () => {
const [securityScheme, req] = generateSecuritySchemeFixture(permission, [recruteurUserO1E1R1], location)
await expect(
authorizationnMiddleware(
{
method: "get",
path: "/path",
securityScheme,
},
{
user: {
type: "IUserRecruteur",
value: opcoUserO1U1,
},
...req,
}
)
).resolves.toBe(undefined)
})
})

describe.each<[Permission]>([["recruiter:manage"], ["recruiter:validate"], ["recruiter:add_job"], ["admin"]])("I do not have %s permission", (permission) => {
it("on recruiter from other Opco", async () => {
Expand Down Expand Up @@ -542,7 +563,7 @@ describe("authorisationService", () => {
})
})

describe.each<[Permission]>([["user:manage"], ["admin"]])("I do not have %s permission", (permission) => {
describe.each<[Permission]>([["admin"]])("I do not have %s permission", (permission) => {
it("on user recruiter from my Opco", async () => {
const [securityScheme, req] = generateSecuritySchemeFixture(permission, [recruteurUserO1E1R1], location)
await expect(
Expand Down
18 changes: 8 additions & 10 deletions shared/routes/user.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ export const zUserRecruteurRoutes = {
})
.strict(),
},
securityScheme: null,
// KBA hotfix: not working
// securityScheme: {
// auth: "cookie-session",
// access: "user:manage",
// ressources: {
// user: [{ opco: { type: "query", key: "opco" } }],
// recruiter: [{ opco: { type: "query", key: "opco" } }],
// },
// },
securityScheme: {
auth: "cookie-session",
access: { every: ["user:manage", "recruiter:manage"] },
ressources: {
user: [{ opco: { type: "query", key: "opco" } }],
recruiter: [{ opco: { type: "query", key: "opco" } }],
},
},
},
"/user": {
method: "get",
Expand Down

0 comments on commit 9ff2417

Please sign in to comment.