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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a9092f2f97e6a3b76afc8339f023be19daae8f43
4
- data.tar.gz: a341795e26fceb21541c1d4dbfabc8acaa5c4942
3
+ metadata.gz: ea91874d47681042c3ee482de3312406ca7978df
4
+ data.tar.gz: 08770d172965ab3bbd1a75748d91bafea2e80afb
5
5
  SHA512:
6
- metadata.gz: fb99bd92c87a8dd3c80a86a9edfaeffd2ebb7beffe321b266126adab91d73f154fc2f53b53c0dcf000801e7e4009278e49151b6fe6c41ef63af27e05626d4e03
7
- data.tar.gz: 9f0f7f2cff61e2f3447de4d4ca00071a2ab897556c274f80bea6dc5681bbc95b708e2cf2ffa550152bb2d4b20f4acaf27ae00619b9c54f024336f305b6f4c11c
6
+ metadata.gz: 288fc8a1586feda6a2ddb47e46c3e653c51c5aec1180c4096dcb78bdc29ad1bee4e32f91ba7b432bd3544dea94e3f654b44553c8a3e10a707fcd42344e116662
7
+ data.tar.gz: f34511cfbba2770ec4b7b292f3949c520791864d9417f0befca20536fce94f62aa5e456b0eb3a9563566a80ebd4cfe563af52623721e641945c28a4fbffb9d13
data/lib/mumukit/auth.rb CHANGED
@@ -3,6 +3,7 @@ require 'mumukit/core'
3
3
  require 'daybreak'
4
4
 
5
5
  require_relative './auth/array'
6
+ require_relative './auth/role'
6
7
  require_relative './auth/roles'
7
8
  require_relative './auth/slug'
8
9
  require_relative './auth/version'
@@ -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
- !!scope_for(role)&.allows?(resource_slug)
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] || Mumukit::Auth::Scope.new
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
@@ -31,7 +31,7 @@ module Mumukit::Auth
31
31
  to_s.present?
32
32
  end
33
33
 
34
- def self.parse(string)
34
+ def self.parse(string='')
35
35
  new(string.split(':').map(&:to_mumukit_grant))
36
36
  end
37
37
 
@@ -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
- ensure
13
- store.close
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
@@ -1,5 +1,5 @@
1
1
  module Mumukit
2
2
  module Auth
3
- VERSION = '3.0.0'
3
+ VERSION = '3.1.0'
4
4
  end
5
5
  end
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.0.0
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