|
1 | 1 | require "../user" |
| 2 | +require "../password" |
| 3 | +require "../../tag" |
2 | 4 |
|
3 | 5 | module LavinMQ |
4 | 6 | module Auth |
5 | 7 | module Users |
6 | 8 | class TempUser < User |
| 9 | + include SortableJSON |
| 10 | + |
| 11 | + getter name, password, permissions |
| 12 | + property tags |
| 13 | + alias Permissions = NamedTuple(config: Regex, read: Regex, write: Regex) |
| 14 | + |
| 15 | + @name : String |
| 16 | + @permissions = Hash(String, Permissions).new |
| 17 | + @password : Password? = nil |
| 18 | + @tags = Array(Tag).new |
| 19 | + @expiration_time : Time? |
| 20 | + |
| 21 | + def initialize |
| 22 | + pp "hello" |
| 23 | + @name = "guest" |
| 24 | + @password = Password::MD5Password.create("guest") |
| 25 | + @tags = [Tag::Administrator] |
| 26 | + @permissions["/"] = {config: /.*/, read: /.*/, write: /.*/} |
| 27 | + @expiration_time = nil |
| 28 | + end |
| 29 | + |
| 30 | + def set_expiration(time : Time) |
| 31 | + @expiration_time = time |
| 32 | + end |
| 33 | + |
| 34 | + def expired? : Bool |
| 35 | + return false unless @expiration_time |
| 36 | + Time.utc > @expiration_time.not_nil! |
| 37 | + end |
| 38 | + |
| 39 | + def can_write?(vhost, name) : Bool |
| 40 | + pp "hellooo" |
| 41 | + return false if expired? |
| 42 | + perm = permissions[vhost]? |
| 43 | + perm ? perm_match?(perm[:write], name) : false |
| 44 | + end |
| 45 | + |
| 46 | + def can_read?(vhost, name) : Bool |
| 47 | + return false if expired? |
| 48 | + perm = permissions[vhost]? |
| 49 | + perm ? perm_match?(perm[:read], name) : false |
| 50 | + end |
| 51 | + |
| 52 | + def can_config?(vhost, name) : Bool |
| 53 | + return false if expired? |
| 54 | + perm = permissions[vhost]? |
| 55 | + perm ? perm_match?(perm[:config], name) : false |
| 56 | + end |
| 57 | + |
| 58 | + def can_impersonate? |
| 59 | + return false if expired? |
| 60 | + @tags.includes? Tag::Impersonator |
| 61 | + end |
| 62 | + |
| 63 | + def hidden? |
| 64 | + false |
| 65 | + end |
| 66 | + |
| 67 | + def details_tuple |
| 68 | + user_details.merge(permissions: @permissions) |
| 69 | + end |
| 70 | + |
| 71 | + def user_details |
| 72 | + { |
| 73 | + name: @name, |
| 74 | + password_hash: @password, |
| 75 | + hashing_algorithm: @password.try &.hash_algorithm, |
| 76 | + tags: @tags.map(&.to_s.downcase).join(","), |
| 77 | + } |
| 78 | + end |
| 79 | + |
| 80 | + def permissions_details |
| 81 | + @permissions.map { |k, p| permissions_details(k, p) } |
| 82 | + end |
| 83 | + |
| 84 | + def permissions_details(vhost, p) |
| 85 | + { |
| 86 | + user: @name, |
| 87 | + vhost: vhost, |
| 88 | + configure: p[:config], |
| 89 | + read: p[:read], |
| 90 | + write: p[:write], |
| 91 | + } |
| 92 | + end |
| 93 | + |
| 94 | + private def perm_match?(perm, name) |
| 95 | + perm != /^$/ && perm != // && perm.matches? name |
| 96 | + end |
7 | 97 | end |
8 | 98 | end |
9 | 99 | end |
|
0 commit comments