Class: CantangoEditor::Permissions

Inherits:
Object
  • Object
show all
Defined in:
app/models/cantango_editor/permissions.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.permissionsObject

Returns the value of attribute permissions.



5
6
7
# File 'app/models/cantango_editor/permissions.rb', line 5

def permissions
  @permissions
end

Class Method Details

.configurationObject



14
15
16
# File 'app/models/cantango_editor/permissions.rb', line 14

def configuration
  CantangoEditor::Configuration
end

.create_empty_permissions_fileObject



106
107
108
109
110
111
112
# File 'app/models/cantango_editor/permissions.rb', line 106

def create_empty_permissions_file
  File.open(permissions_file_path, 'w') do |file|
    YAML.dump(permission_types_nil_hash, file)
  end

  permissions_file_path
end

.models_available_namesObject



18
19
20
# File 'app/models/cantango_editor/permissions.rb', line 18

def models_available_names
  models_available.map(&:name).unshift "all"
end

.permission_types_nil_hashObject



22
23
24
25
26
27
28
29
30
# File 'app/models/cantango_editor/permissions.rb', line 22

def permission_types_nil_hash
  permission_types_available.inject({}) do |result_hash, pt|
    roles_hash = (permission_groups_available[pt] || []).inject({}) do |rh, r|
      rh.merge({r.to_s => nil})
    end
    
    result_hash.merge!({pt.to_s => roles_hash})
  end
end

.permissions_fileObject



100
101
102
103
104
# File 'app/models/cantango_editor/permissions.rb', line 100

def permissions_file
  create_empty_permissions_file if !File.file? permissions_file_path
  
  permissions_file_path
end

.permissions_file_pathObject



114
115
116
# File 'app/models/cantango_editor/permissions.rb', line 114

def permissions_file_path
  File.join Rails.root + "config/" + "permissions.yml"
end

.persist_permissions!Object



63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/cantango_editor/permissions.rb', line 63

def persist_permissions!
 
  yml = YAML.dump(permissions)

  # Sanitize permissions hash
  yml.gsub!(/\!map.*\s/,"\n")

  File.open(permissions_file, 'w') do |out|
    out.write yml
  end
end

.raw_file_contentObject



75
76
77
78
79
80
81
82
83
84
# File 'app/models/cantango_editor/permissions.rb', line 75

def raw_file_content
  raw_content = ""
  File.open(permissions_file, 'r') do |f|
    raw_content = f.read
  end
     
  raw_content
rescue => e
  raise e
end

.remove_and_save_permissions(permissions_hash) ⇒ Object



49
50
51
52
53
54
# File 'app/models/cantango_editor/permissions.rb', line 49

def remove_and_save_permissions permissions_hash
  return unless permissions_hash
  
  update_remove_permissions permissions_hash
  persist_permissions! 
end

.save_new_permissions(permissions_hash) ⇒ Object



56
57
58
59
60
61
# File 'app/models/cantango_editor/permissions.rb', line 56

def save_new_permissions permissions_hash
  return unless permissions_hash

  update_new_permissions permissions_hash
  persist_permissions! 
end

.update_new_permissions(permissions_hash) ⇒ Object



41
42
43
# File 'app/models/cantango_editor/permissions.rb', line 41

def update_new_permissions permissions_hash
  permissions.deep_merge_permissions! permissions_hash
end

.update_permissions!(params) ⇒ Object



36
37
38
39
# File 'app/models/cantango_editor/permissions.rb', line 36

def update_permissions! params
  save_new_permissions params[:new_permissions]
  remove_and_save_permissions params[:delete_permissions]
end

.update_remove_permissions(permissions_hash) ⇒ Object



45
46
47
# File 'app/models/cantango_editor/permissions.rb', line 45

def update_remove_permissions permissions_hash
  permissions.deep_remove_permissions! permissions_hash
end

.validate_content(yml_content) ⇒ Object



95
96
97
98
# File 'app/models/cantango_editor/permissions.rb', line 95

def validate_content yml_content
  raise "#{permissions_file} should contain Hash-based information" unless yml_content.is_a?(Hash)
  raise "#{permissions_file} should not contain permission_types not listed in #permission_types_available" if (yml_content.keys - permission_types_available.to_strings).size > 0
end

.yml_file_contentObject



86
87
88
89
90
91
92
93
# File 'app/models/cantango_editor/permissions.rb', line 86

def yml_file_content
  yml_content = YAML.load_file(permissions_file)
  validate_content yml_content

  PermissionsHash[yml_content]
rescue => e
  raise e
end