Class: Mumukit::Auth::Permissions::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/mumuki/classroom/permissions_diff.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDiff

Returns a new instance of Diff.



47
48
49
# File 'lib/mumuki/classroom/permissions_diff.rb', line 47

def initialize
  @changes = []
end

Instance Attribute Details

#changesObject

Returns the value of attribute changes.



45
46
47
# File 'lib/mumuki/classroom/permissions_diff.rb', line 45

def changes
  @changes
end

Class Method Details

.diff(old_permissions, new_permissions) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mumuki/classroom/permissions_diff.rb', line 66

def self.diff(old_permissions, new_permissions)
  return Mumukit::Auth::Permissions::Diff.new if new_permissions.nil?

  old_permissions = old_permissions.to_mumukit_permissions
  new_permissions = new_permissions.to_mumukit_permissions
  new.tap do |it|
    Mumukit::Auth::Roles::ROLES.each do |role|
      it.compare_grants! role, old_permissions, new_permissions, :removed
      it.compare_grants! role, new_permissions, old_permissions, :added
    end
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Object



79
80
81
# File 'lib/mumuki/classroom/permissions_diff.rb', line 79

def as_json(options = {})
  {changes: @changes}.as_json options
end

#changes_by_organizationObject



51
52
53
# File 'lib/mumuki/classroom/permissions_diff.rb', line 51

def changes_by_organization
  changes.group_by(&:organization).with_indifferent_access
end

#compare_grants!(role, some_permissions, another_permissions, change_type) ⇒ Object



59
60
61
62
63
64
# File 'lib/mumuki/classroom/permissions_diff.rb', line 59

def compare_grants!(role, some_permissions, another_permissions, change_type)
  some_permissions
    .grants_for(role)
    .select { |grant| !another_permissions.role_allows?(role, grant) }
    .each { |grant| changes << Change.new(role, grant, change_type) }
end

#empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/mumuki/classroom/permissions_diff.rb', line 55

def empty?
  changes.empty?
end