mumukit-auth 3.0.0 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mumukit/auth.rb +1 -0
- data/lib/mumukit/auth/permissions.rb +8 -4
- data/lib/mumukit/auth/role.rb +57 -0
- data/lib/mumukit/auth/scope.rb +1 -1
- data/lib/mumukit/auth/store.rb +17 -18
- data/lib/mumukit/auth/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea91874d47681042c3ee482de3312406ca7978df
|
4
|
+
data.tar.gz: 08770d172965ab3bbd1a75748d91bafea2e80afb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 288fc8a1586feda6a2ddb47e46c3e653c51c5aec1180c4096dcb78bdc29ad1bee4e32f91ba7b432bd3544dea94e3f654b44553c8a3e10a707fcd42344e116662
|
7
|
+
data.tar.gz: f34511cfbba2770ec4b7b292f3949c520791864d9417f0befca20536fce94f62aa5e456b0eb3a9563566a80ebd4cfe563af52623721e641945c28a4fbffb9d13
|
data/lib/mumukit/auth.rb
CHANGED
@@ -4,13 +4,17 @@ class Mumukit::Auth::Permissions
|
|
4
4
|
attr_accessor :scopes
|
5
5
|
|
6
6
|
def initialize(scopes={})
|
7
|
-
raise 'invalid scopes' if scopes.any? { |key, value| value.class != Mumukit::Auth::Scope
|
7
|
+
raise 'invalid scopes' if scopes.any? { |key, value| value.class != Mumukit::Auth::Scope }
|
8
8
|
|
9
9
|
@scopes = scopes.with_indifferent_access
|
10
10
|
end
|
11
11
|
|
12
12
|
def has_permission?(role, resource_slug)
|
13
|
-
|
13
|
+
Mumukit::Auth::Role.parse(role).allows?(resource_slug, self)
|
14
|
+
end
|
15
|
+
|
16
|
+
def role_allows?(role, resource_slug)
|
17
|
+
scope_for(role).allows?(resource_slug)
|
14
18
|
end
|
15
19
|
|
16
20
|
def protect!(scope, slug)
|
@@ -21,12 +25,12 @@ class Mumukit::Auth::Permissions
|
|
21
25
|
scopes[role].present?
|
22
26
|
end
|
23
27
|
|
28
|
+
|
24
29
|
def scope_for(role)
|
25
|
-
self.scopes[role]
|
30
|
+
self.scopes[role] ||= Mumukit::Auth::Scope.new
|
26
31
|
end
|
27
32
|
|
28
33
|
def add_permission!(role, *grants)
|
29
|
-
self.scopes[role] ||= Mumukit::Auth::Scope.new
|
30
34
|
scope_for(role)&.add_grant! *grants
|
31
35
|
end
|
32
36
|
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Mumukit::Auth
|
2
|
+
class Role
|
3
|
+
def initialize(symbol)
|
4
|
+
@symbol=symbol
|
5
|
+
end
|
6
|
+
|
7
|
+
def allows?(resource_slug, permissions)
|
8
|
+
permissions.role_allows?(to_sym, resource_slug) ||
|
9
|
+
parent_allows?(resource_slug, permissions)
|
10
|
+
end
|
11
|
+
|
12
|
+
def parent_allows?(resource_slug, permissions)
|
13
|
+
parent.allows?(resource_slug, permissions)
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_sym
|
17
|
+
@symbol
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def self.parent(parent)
|
23
|
+
define_method(:parent) { self.class.parse(parent) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.parse(role)
|
27
|
+
@roles ||= {}
|
28
|
+
@roles[role] ||= "Mumukit::Auth::Role::#{role.to_s.camelize}".constantize.new(role.to_sym)
|
29
|
+
end
|
30
|
+
|
31
|
+
class Student < Role
|
32
|
+
parent :owner
|
33
|
+
end
|
34
|
+
class Teacher < Role
|
35
|
+
parent :headmaster
|
36
|
+
end
|
37
|
+
class Headmaster < Role
|
38
|
+
parent :owner
|
39
|
+
end
|
40
|
+
class Writer < Role
|
41
|
+
parent :editor
|
42
|
+
end
|
43
|
+
class Editor < Role
|
44
|
+
parent :owner
|
45
|
+
end
|
46
|
+
class Janitor < Role
|
47
|
+
parent :owner
|
48
|
+
end
|
49
|
+
class Owner < Role
|
50
|
+
parent nil
|
51
|
+
|
52
|
+
def parent_allows?(*)
|
53
|
+
false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/mumukit/auth/scope.rb
CHANGED
data/lib/mumukit/auth/store.rb
CHANGED
@@ -1,5 +1,20 @@
|
|
1
1
|
module Mumukit::Auth
|
2
2
|
class Store
|
3
|
+
def initialize(db_name)
|
4
|
+
@db = Daybreak::DB.new "#{db_name}.db", default: '{}'
|
5
|
+
end
|
6
|
+
|
7
|
+
def close
|
8
|
+
@db.close
|
9
|
+
end
|
10
|
+
|
11
|
+
def set!(key, value)
|
12
|
+
@db.update! key.to_sym => value.to_json
|
13
|
+
end
|
14
|
+
|
15
|
+
def get(key)
|
16
|
+
Mumukit::Auth::Permissions.load @db[key]
|
17
|
+
end
|
3
18
|
|
4
19
|
class << self
|
5
20
|
def from_env
|
@@ -9,8 +24,8 @@ module Mumukit::Auth
|
|
9
24
|
def with(&block)
|
10
25
|
store = from_env
|
11
26
|
block.call store
|
12
|
-
|
13
|
-
|
27
|
+
ensure
|
28
|
+
store.close
|
14
29
|
end
|
15
30
|
|
16
31
|
def set!(*args)
|
@@ -21,21 +36,5 @@ module Mumukit::Auth
|
|
21
36
|
with { |store| store.get(key) }
|
22
37
|
end
|
23
38
|
end
|
24
|
-
|
25
|
-
def initialize(db_name)
|
26
|
-
@db = Daybreak::DB.new "#{db_name}.db", default: '{}'
|
27
|
-
end
|
28
|
-
|
29
|
-
def close
|
30
|
-
@db.close
|
31
|
-
end
|
32
|
-
|
33
|
-
def set!(key, value)
|
34
|
-
@db.update! key.to_sym => value.to_json
|
35
|
-
end
|
36
|
-
|
37
|
-
def get(key)
|
38
|
-
Mumukit::Auth::Permissions.load @db[key]
|
39
|
-
end
|
40
39
|
end
|
41
40
|
end
|
data/lib/mumukit/auth/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mumukit-auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Franco Leonardo Bulgarelli
|
@@ -106,6 +106,7 @@ files:
|
|
106
106
|
- lib/mumukit/auth/exceptions.rb
|
107
107
|
- lib/mumukit/auth/grant.rb
|
108
108
|
- lib/mumukit/auth/permissions.rb
|
109
|
+
- lib/mumukit/auth/role.rb
|
109
110
|
- lib/mumukit/auth/roles.rb
|
110
111
|
- lib/mumukit/auth/scope.rb
|
111
112
|
- lib/mumukit/auth/slug.rb
|