gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit3598093635980936inbox: preserve desktop columns after list styles were scopedmre35980936/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

  • 35980936inbox: preserve desktop columns after list styles were scopedmre
  • c077a5cbstyles: format Style with nested language rulesmre
  • ccdde2beticket: format Style with nested language rulesmre
  • d083535bticket_list: nest Style rules and bare media queriesmre
  • c24608ebmarkdown: format Style with nested language rulesmre
  • de964b3bsettings: format Style with nested language rulesmre
  • 03c1de05new_project: format Style with nested language rulesmre
  • bd76cfa6connect: format Style with nested language rulesmre
  • 01fad593inbox: format Style with nested language rulesmre
  • 79dc9392you: format Style with nested language rulesmre
  • 78073baamain: format Style with nested language rulesmre
  • 5499c290Sync theme unchanged from layouts 17e0f03mre
  • fd33ee21Sync reviewed components from 8176eb1mre
  • 93348453Re-vendor tickets to Hybriel set day35mre
  • a4123831tickets#42: the /connect and /login/callback error pages are app pages: the function routes redirect to /connect/failed/<code> and /login/failed/<code> (components/problem.hl, sentences in lib/problems.hl), htmlPage and its hand-written stylesheet are gone; gate tests/error-pages.mjsmre
  • cefc7b4atickets: a startup error prints its message before exit 1 (hybriel#247)mre
  • 99bd6570tickets: lib/migrate.hl writes no table of another topic any more: projects.hl rewriteSlug / addProjectNamed / hasMembers, tickets.hl moveToProject / ticketRecords / ticketRecordsOf, events.hl renameStates / eventRecords / eventRecordsOf; the migration calls them (tickets#47)mre
  • b57161e3tickets: the markup names its own rules (Style.pMessage, Style.olTickets, ...) instead of hand-written class strings that copy a stylesheet rule; the shared bodies sit in lib/looks.hl (tickets#38)mre
  • f767b24dtickets: 111 needless let became plain declarations (3 in ticket.hl, ticket_list.hl, comments.hl and 108 never-reassigned, not captured loop-body ones); the 212 really reassigned lets stay (tickets#44)mre
  • 16c0d3b5tickets: the modal window is phone first: the full-screen sheet is the base rule, the centred window sits in the min-width 60rem block (tickets#39)mre