gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit772e0696772e0696|||PR Allow project admins to move tickets between their projectsmre772e0696/lib/invites.hl

4.3 KB

  1. // lib/invites.hl — PEOPLE JOIN A PROJECT THROUGH IDENT'S INVITE SERVICE (ticket #20; ident README "Invites",
  2. // ident#22). An admin asks ident for an invite for one project and a role (mayInvite, createInvite) and shows the
  3. // link; whoever opens it and picks an identity is sent back to /login/callback?ident_code=…&invite=<invite id>.
  4. // lib/api.hl loginCallback then exchanges the code (users.hl), and joinByInvite asks ident who ACCEPTED the invite
  5. // (`identities`) and only then makes that person a member. Statics only, the server realm. A failed fetch is
  6. // absorbed by project.hl's `on Error` (the call then yields null → "ident did not answer").
  7. import { fetch } from 'hl:fetch'
  8. import { identKey, identSecret, identExchangeUrl, callbackUrl } from './users.hl'
  9. import { projectByRef, isAdminOf, roles, addMember } from './projects.hl'
  10. static ua = { 'user-agent' = 'tickets.worldapi.org (ident invites)' }
  11. // only letters, digits and dashes (ident's invite ids)
  12. static isInviteId = (s) => {
  13. if (s == null || hlTypeName(s) != 'String' || s.length == 0 || s.length > 100) { return false }
  14. let i = 0
  15. while (i < s.length) {
  16. c = s.charCodeAt(i)
  17. if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 122) || (c >= 65 && c <= 90) || c == 45 || c == 95)) { return false }
  18. i = i + 1
  19. }
  20. return true
  21. }
  22. static identCall = (path, body) => {
  23. if (identKey == '' || identSecret == '') { return { error = 'invites are not set up on this server (IDENT_API_KEY / IDENT_API_SECRET missing)' } }
  24. let json = { key = identKey secret = identSecret }
  25. for (k of body.keys()) { json[k] = body[k] }
  26. r = fetch(identExchangeUrl + path, { method = 'POST' json = json headers = ua timeoutMs = 10000 })
  27. if (r == null || r.status == null || r.status == 0) { return { error = 'ident did not answer' } }
  28. j = r.json()
  29. if (r.status != 200) { return { error = 'ident refused (' + r.status + (j != null && j.error != null ? ': ' + j.error : '') + ')' } }
  30. if (j == null) { return { error = 'ident answered nothing readable' } }
  31. return { body = j }
  32. }
  33. // may this user invite to project `p` with `role`? null, or the refusal { error, field, forbidden? }
  34. // (the caller then checks its numbers and calls createInvite)
  35. static mayInvite = (p, user, role) => {
  36. if (!isAdminOf(p.id, user)) { return { error = 'only an admin of the project can invite' field = '' forbidden = true } }
  37. if (!roles.includes(role)) { return { error = 'role must be one of: ' + roles.join(', ') field = 'role' } }
  38. return null
  39. }
  40. // an invite link for `projectId` and `role`: { url, id, expires } | { error }. `email` (optional) makes ident mail it.
  41. static createInvite = (projectId, role, uses, days, email) => {
  42. body = { project = projectId role = role return = callbackUrl uses = uses days = days }
  43. if (email != null && email != '') { body.email = email }
  44. r = identCall('/api/invites', body)
  45. if (r.error != null) { return r }
  46. j = r.body
  47. if (j.url == null || j.id == null) { return { error = 'ident answered no invite link' } }
  48. return { url = j.url id = j.id expires = j.expires mailed = j.mailed == true }
  49. }
  50. // did this identity accept this invite? { project (id), role } | { error }
  51. static acceptedInvite = (inviteId, identity) => {
  52. if (!isInviteId(inviteId)) { return { error = 'that is not an invite id' } }
  53. r = identCall('/api/invites/get', { id = inviteId })
  54. if (r.error != null) { return r }
  55. inv = r.body.invite
  56. if (inv == null || inv.project == null || inv.role == null) { return { error = 'ident answered no invite' } }
  57. let ok = false
  58. if (inv.identities != null && countOfIds(inv.identities) > 0) { for (i of inv.identities) { if (i == identity) { ok = true } } }
  59. if (!ok) { return { error = 'this login did not accept that invite' } }
  60. return { project = inv.project role = inv.role }
  61. }
  62. static countOfIds = (list) => {
  63. n = list.length
  64. return n == null ? 0 : n
  65. }
  66. // THE INVITED PERSON JOINS (the login callback brought `invite=<id>`): only when ident says this identity accepted
  67. // the invite does the user become a member, with the invite's role. Answers { project (record) } or { error }
  68. static joinByInvite = (inviteId, identity, userId) => {
  69. a = acceptedInvite(inviteId, identity)
  70. if (a.error != null) { return a }
  71. jp = projectByRef(a.project)
  72. if (jp == null || !roles.includes(a.role)) { return { error = 'the invite is for a project or role that does not exist here' } }
  73. addMember(jp.id, userId, a.role)
  74. return { project = jp }
  75. }

Branches

Latest commits

  • 772e0696|||PR Allow project admins to move tickets between their projectsmre
  • be92a560Merge branch 'records' into mainTickets
  • 0daf2ea8|||PR records: PR-only workflow, #50 donemre
  • 0185d0b6Log PR 1 deploymre
  • f5bf16e2Mission 031: deploy PR 1mre
  • c69cd5f3Merge branch 'pr-flow' into mainTickets
  • cc56798cLog gitoria PR-only requestmre
  • d876d0b2|||PR README: changes land as PRsmre
  • fd83ac1fconductor: caramboleyo is the user, not owner/creatorApollo
  • 3b858c91Log backup hard-link fixmre
  • 7f6b37a0deploy.sh backup: tar --hard-dereference (hard links broke mpackdb sidecars on restore)mre
  • e93711d9Log E30 livemre
  • 59c02e4dMerge e30: preserve edited state note labels in Markdownmre
  • 30970796Log E30 deploy GOmre
  • 7c8e6d83Limit edited state note fix to Markdown renderingmre
  • 910536d0Log E30 reviewmre
  • 36984386Preserve state transitions when their notes are editedmre
  • d75cb946Log E30 startmre
  • 0bf3fefaMission 030: edited state note labelmre
  • 691565c6Log neutral-ticket rule checkmre