Improved user and group removal, simpler APIs.

This commit is contained in:
Luka Vandervelden 2019-11-17 19:05:22 +01:00
parent 02c12703dd
commit 313c9edfd0

View File

@ -56,9 +56,9 @@ class Passwd
false false
end end
def get_user(uid : Int32) : Passwd::User? def get_user(&block) : Passwd::User?
each_user do |user| each_user do |user|
if user.uid == uid if yield user
set_user_groups user set_user_groups user
return user return user
@ -66,21 +66,24 @@ class Passwd
end end
end end
def get_user(uid : Int32)
get_user &.uid.==(uid)
end
def get_user(login : String)
get_user &.login.==(login)
end
## ##
# Will fail if the user is found but the password is invalid. # Will fail if the user is found but the password is invalid.
def get_user(login : String, password : String) : Passwd::User? # Use this method if you somehow need to check the password…
# Be careful though, your system probably does not use SHA256 hashes as
# passwords. You should use this only on your own passwd and group files.
def get_user(login : String, password : String)
hash = Passwd.hash_password password hash = Passwd.hash_password password
each_user do |user| get_user do |user|
if user.login == login user.login == login && user.password_hash == hash
if user.password_hash == hash
set_user_groups user
return user
end
next
end
end end
end end
@ -201,27 +204,26 @@ class Passwd
FileUtils.cp tempfile.path, path FileUtils.cp tempfile.path, path
end end
def remove_user(uid) def remove_user_from_groups(user)
user = get_user(uid)
return nil unless user
user_login = user.login
new_group = group_as_array.map do |line| new_group = group_as_array.map do |line|
group = Group.new line group = Group.new line
group.users.select! &.!=(user_login) group.users.select! &.!=(user.login)
group.users.select! &.!=(user.uid.to_s)
group.to_csv group.to_csv
end end
safe_rewrite @group, new_group.join('\n') + '\n' safe_rewrite @group, new_group.join('\n') + '\n'
end
def remove_user(&block)
new_passwd = passwd_as_array.compact_map do |line| new_passwd = passwd_as_array.compact_map do |line|
user = User.new line user = User.new line
if uid == user.uid if yield user
remove_user_from_groups user
nil nil
else else
line.join ':' line.join ':'
@ -231,11 +233,19 @@ class Passwd
safe_rewrite @passwd, new_passwd.join("\n") + "\n" safe_rewrite @passwd, new_passwd.join("\n") + "\n"
end end
def remove_group(gid) def remove_user(uid : Int32)
remove_user &.uid.==(uid)
end
def remove_user(login : String)
remove_user &.login.==(login)
end
def remove_group(&block)
new_group = group_as_array.compact_map do |line| new_group = group_as_array.compact_map do |line|
group = Group.new line group = Group.new line
if gid == group.gid if yield group
nil nil
else else
line.join ':' line.join ':'
@ -244,5 +254,13 @@ class Passwd
safe_rewrite @group, new_group.join('\n') + '\n' safe_rewrite @group, new_group.join('\n') + '\n'
end end
def remove_group(gid : Int32)
remove_group &.gid.==(gid)
end
def remove_group(name : String)
remove_group &.name.==(name)
end
end end