Class: Mumukit::Auth::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/mumukit/auth/metadata.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Metadata

Returns a new instance of Metadata.



2
3
4
# File 'lib/mumukit/auth/metadata.rb', line 2

def initialize(json)
  @json = json
end

Class Method Details

.dump(metadata) ⇒ Object



61
62
63
# File 'lib/mumukit/auth/metadata.rb', line 61

def self.dump()
  .to_json
end

.load(json) ⇒ Object



57
58
59
# File 'lib/mumukit/auth/metadata.rb', line 57

def self.load(json)
  new(JSON.parse(json))
end

Instance Method Details

#add_permission!(app, permission) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/mumukit/auth/metadata.rb', line 14

def add_permission!(app, permission)
  if permissions(app).present?
    @json[app] = process_add_permission(app, permission)
  else
    @json.merge!("#{app}" => {'permissions' => permission})
  end
end

#admin?(slug) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/mumukit/auth/metadata.rb', line 45

def admin?(slug)
  has_role? 'admin', slug
end

#as_json(_options = {}) ⇒ Object



6
7
8
# File 'lib/mumukit/auth/metadata.rb', line 6

def as_json(_options={})
  @json
end

#librarian?(slug) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/mumukit/auth/metadata.rb', line 41

def librarian?(slug)
  has_role? 'bibliotheca', slug
end

#permissions(app) ⇒ Object



10
11
12
# File 'lib/mumukit/auth/metadata.rb', line 10

def permissions(app)
  @json.dig(app, 'permissions').to_mumukit_auth_permissions
end

#process_add_permission(app, permission) ⇒ Object



37
38
39
# File 'lib/mumukit/auth/metadata.rb', line 37

def process_add_permission(app, permission)
  process_permission(permissions(app).as_json + ":#{permission}")
end

#process_permission(new_permissions) ⇒ Object



29
30
31
# File 'lib/mumukit/auth/metadata.rb', line 29

def process_permission(new_permissions)
  {'permissions' => Mumukit::Auth::Permissions.load(new_permissions).to_s}
end

#process_remove_permission(app, permission) ⇒ Object



33
34
35
# File 'lib/mumukit/auth/metadata.rb', line 33

def process_remove_permission(app, permission)
  process_permission(permissions(app).as_json.split(':').reject { |it| it == permission }.join(':'))
end

#remove_permission!(app, permission) ⇒ Object



22
23
24
25
26
27
# File 'lib/mumukit/auth/metadata.rb', line 22

def remove_permission!(app, permission)
  if permissions(app).present?
    @json[app] = process_remove_permission(app, permission)
  end
  @json.delete(app) if @json.dig(app, 'permissions').blank?
end

#student?(slug) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/mumukit/auth/metadata.rb', line 53

def student?(slug)
  has_role? 'atheneum', slug
end

#teacher?(slug) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/mumukit/auth/metadata.rb', line 49

def teacher?(slug)
  has_role? 'classroom', slug
end