oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "context"
5 "encoding/json"
6
7 "amadan.net/oficina/equipo/internal/wire"
8)
9
10func (s *Store) AuthorizeAccess(ctx context.Context, l Lease, id string) (refused string, err error) {
11 err = s.tx(ctx, func(t *txn) error {
12 if _, err := t.current(l); err != nil {
13 return err
14 }
15 var err error
16 refused, err = t.authorizeAccess(l, id)
17 return err
18 })
19 return
20}
21
22func (t *txn) authorizeAccess(l Lease, id string) (string, error) {
23 if l.Protocol < wire.ProtocolAccess {
24 return "withdrawn", nil
25 }
26 var c cardRecord
27 if err := t.db.Where("id = ? AND person = ? AND kind = ?", id, l.Runner.Person, wire.CardAccess).First(&c).Error; err != nil {
28 return "", err
29 }
30 var a wire.AccessRequest
31 if json.Unmarshal([]byte(c.Access), &a) != nil {
32 return "", ErrInvalid
33 }
34 if c.State != wire.CardOpen || a.State != "filing" {
35 return "withdrawn", nil
36 }
37 // Only the runner that received this confirmed command can settle its refusal.
38 var n int64
39 if err := t.db.Model(&commandRecord{}).Where("runner_id = ? AND person = ? AND kind = ? AND json_extract(body, '$.card_id') = ?", l.Runner.RunnerID, l.Runner.Person, wire.FrameRequestAccess, id).Count(&n).Error; err != nil {
40 return "", err
41 }
42 if n == 0 {
43 return "", ErrForbidden
44 }
45 primary, err := t.s.authority.PrimaryRunner(t.db, l.Runner.Person)
46 if err != nil {
47 return "", err
48 }
49 role := wire.RolePrimary
50 if t.s.authority.RunnerRole != nil {
51 role, err = t.s.authority.RunnerRole(t.db, l.Runner.RunnerID)
52 if err != nil {
53 return "", err
54 }
55 }
56 if primary == l.Runner.RunnerID && role == wire.RolePrimary {
57 return "", nil
58 }
59 a.State = "authority"
60 raw, _ := json.Marshal(a)
61 c.Access = string(raw)
62 c.State = wire.CardResolved
63 if err := t.saveCard(&c); err != nil {
64 return "", err
65 }
66 return "withdrawn", t.s.authority.Audit(t.db, c.Person, "access.authority", c.ID)
67}
68