/
config.rb
235 lines (197 loc) · 5.8 KB
/
config.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
module Y2Users
# Class to represent a configuration of users and groups
#
# @example
# user1 = User.new("john")
# user2 = User.new("peter")
# group = Group.new("users")
#
# config1 = Config.new("config1")
# config1.users #=> []
# config1.attach(user1, user2, group)
# config1.users #=> [user1, user2]
# config1.groups #=> [group]
#
# config2 = config1.clone_as("config2")
# user = config2.users.first
# config2.detach(user)
# config2.users #=> [user2]
class Config
class << self
def get(name)
@register ||= {}
@register[name]
end
def register(config)
@register ||= {}
@register[config.name] = config
end
def remove(config)
name = config.is_a?(self) ? config.name : config
@register.delete(name)
end
def system(reader: nil, force_read: false)
res = get(:system)
return res if res && !force_read
if !reader
require "y2users/linux/reader"
reader = Linux::Reader.new
end
res = new(:system)
reader.read_to(res)
res
end
end
# Config name
#
# @return [String]
attr_reader :name
# Constructor
#
# param name [String]
def initialize(name)
@name = name
@users_manager = ElementManager.new(self)
@groups_manager = ElementManager.new(self)
self.class.register(self)
end
# Users that belong to this config
#
# @note The list of users cannot be modified directly. Use {#attach} and {#detach} instead.
#
# @return [Array<User>]
def users
users_manager.elements.dup.freeze
end
# Groups that belong to this config
#
# @note The list of groups cannot be modified directly. Use {#attach} and {#detach} instead.
#
# @return [Array<Group>]
def groups
groups_manager.elements.dup.freeze
end
# Attaches users and groups to this config
#
# The given users and groups cannot be already attached to a config.
#
# @param elements [Array<User, Group>]
def attach(*elements)
elements.each { |e| attach_element(e) }
end
# Detaches users and groups from this config
#
# @param elements [Array<User, Group>]
def detach(*elements)
elements.each { |e| detach_element(e) }
end
# Generates a new config with the very same list of users and groups
#
# Note that the cloned users and groups keep the same id as the original users and groups.
#
# @param name [String] name for the new cloned config
# @return [Config]
def clone_as(name)
config = self.class.new(name)
elements = users + groups
elements.each { |e| config.clone_element(e) }
config
end
# Generates the id for the next attached user or group
#
# FIXME: This method should not be public
#
# @return [Integer]
def self.next_element_id
@last_element_id ||= 0
@last_element_id += 1
end
protected
# Clones a given user or group and attaches it into this config
#
# Note that the cloned element keep the same id as the source element.
#
# @param element [User, Group]
def clone_element(element)
cloned = element.clone
cloned.assign_internal_id(element.id)
attach(cloned)
end
private
# Manager for users
#
# @return [ElementManager]
attr_reader :users_manager
# Manager for groups
#
# @return [ElementManager]
attr_reader :groups_manager
# Attaches an user or group
#
# An id is assigned to the given user/group, if needed.
#
# @param element [User, Group]
def attach_element(element)
element.assign_internal_id(self.class.next_element_id) if element.id.nil?
element.is_a?(User) ? users_manager.attach(element) : groups_manager.attach(element)
end
# Detaches an user or group
#
# @param element [User, Group]
def detach_element(element)
element.is_a?(User) ? users_manager.detach(element) : groups_manager.detach(element)
end
# Helper class to manage a list of users or groups
class ElementManager
# @return [Array<User, Group>]
attr_reader :elements
# @return [Config]
attr_reader :config
# Constructor
#
# @param config [Config]
def initialize(config)
@config = config
@elements = []
end
# Attaches the element to the config
#
# @raise [RuntimeError] if the element is already attached
#
# @param element [User, Group]
def attach(element)
raise "Element already attached: #{element}" if element.attached?
@elements << element
element.assign_config(config)
end
# Detaches the element from the config
#
# @param element [User, Group]
def detach(element)
return if element.config != config
index = @elements.find_index { |e| e.is?(element) }
@elements.delete_at(index) if index
element.assign_config(nil)
element.assign_internal_id(nil)
end
end
end
end