Module: Foreman::DefaultData::Loader

Defined in:
lib/foreman/default_data/loader.rb

Class Method Summary collapse

Class Method Details

.load(reset = false) ⇒ Object

Loads the default data Raises a RecordNotSaved exception if something goes wrong


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/foreman/default_data/loader.rb', line 32

def load(reset=false)

  # We may be executing something like rake db:migrate:reset, which destroys this table; only continue if the table exists
  Role.count rescue return
  Role.transaction do
    # Roles
    manager = Role.where(:name => "Manager").empty? ? Role.create(:name => "Manager") : Role.where(:name => "Manager")[0]
    if reset or manager.permissions.empty?
      manager.update_attribute :permissions, manager.setable_permissions.collect {|p| p.name}
    end

    ptable_editor =  Role.where(:name => "Edit partition tables").empty? ? Role.create(:name => "Edit partition tables") : Role.where(:name => "Edit partition tables")[0]
    if reset or ptable_editor.permissions.empty?
      ptable_editor.update_attribute :permissions, [:view_ptables, :create_ptables, :edit_ptables, :destroy_ptables]
    end

    hosts_reader =  Role.where(:name => "View hosts").empty? ? Role.create(:name => "View hosts") : Role.where(:name => "View hosts")[0]
    if reset or hosts_reader.permissions.empty?
      hosts_reader.update_attribute :permissions, [:view_hosts]
    end

    hosts_editor =  Role.where(:name => "Edit hosts").empty? ? Role.create(:name => "Edit hosts") : Role.where(:name => "Edit hosts")[0]
    if reset or hosts_editor.permissions.empty?
      hosts_editor.update_attribute :permissions, [:view_hosts,    :edit_hosts,    :create_hosts,    :destroy_hosts, :build_hosts]
    end

    viewer =  Role.where(:name => "Viewer").empty? ? Role.create(:name => "Viewer") : Role.where(:name => "Viewer")[0]
    if reset or viewer.permissions.empty?
      viewer.update_attribute :permissions, [:view_hosts,
        :view_puppetclasses,
        :view_hostgroups,
        :view_domains,
        :view_operatingsystems,
        :view_locations,
        :view_media,
        :view_models,
        :view_environments,
        :view_architectures,
        :view_ptables,
        :view_globals,
        :view_external_variables,
        :view_authenticators,
        :access_settings,
        :access_dashboard,
        :view_reports,
        :view_facts,
        :view_smart_proxies,
        :view_subnets,
        :view_statistics,
        :view_organizations,
        :view_usergroups,
        :view_users,
        :view_audit_logs]
    end

    siteman = Role.where(:name => "Site manager").empty? ? Role.create(:name => "Site manager") : Role.where(:name => "Site manager")[0]
    if reset or siteman.permissions.empty?
      siteman.update_attribute :permissions, [ :view_architectures,
        :view_audit_logs,
        :view_authenticators,
        :access_dashboard,
        :view_domains,
        :view_environments,
        :import_environments,
        :view_external_variables,
        :create_external_variables,
        :edit_external_variables,
        :destroy_external_variables,
        :view_facts,
        :view_globals,
        :view_hostgroups,
        :view_hosts,
        :view_smart_proxies_puppetca,
        :view_smart_proxies_autosign,
        :create_hosts,
        :edit_hosts,
        :destroy_hosts,
        :build_hosts,
        :view_media,
        :create_media,
        :edit_media,
        :destroy_media,
        :view_models,
        :view_operatingsystems,
        :view_ptables,
        :view_puppetclasses,
        :import_puppetclasses,
        :view_reports,
        :destroy_reports,
        :access_settings,
        :view_smart_proxies,
        :edit_smart_proxies,
        :view_subnets,
        :edit_subnets,
        :view_statistics,
        :view_usergroups,
        :create_usergroups,
        :edit_usergroups,
        :destroy_usergroups,
        :view_users,
        :edit_users]
    end
    if reset or Role.default_user.permissions.empty?
      Role.default_user.update_attribute :permissions, [:view_hosts,
        :view_puppetclasses,
        :view_hostgroups,
        :view_domains,
        :view_operatingsystems,
        :view_media,
        :view_models,
        :view_environments,
        :view_architectures,
        :view_ptables,
        :view_globals,
        :view_external_variables,
        :view_authenticators,
        :access_settings,
        :access_dashboard,
        :view_reports,
        :view_subnets,
        :view_facts,
        :view_locations,
        :view_organizations,
        :view_statistics]
    end
    if reset or Role.anonymous.permissions.empty?
      Role.anonymous.update_attribute :permissions, [:view_hosts, :view_bookmarks, :view_tasks]
    end
  end
  true
end

.no_data?Boolean

Returns true if no data is already loaded in the database otherwise false


26
27
28
# File 'lib/foreman/default_data/loader.rb', line 26

def no_data?
  !Role.first(:conditions => {:builtin => 0})
end