Added group edition APIs.

Still incomplete, still WIP, but it’s a start.
This commit is contained in:
Luka Vandervelden 2020-01-04 09:09:37 +01:00
parent 4a65a41f16
commit b9568fb33a
3 changed files with 52 additions and 16 deletions

View File

@ -1,10 +1,10 @@
require "csv"
class Passwd::Group
getter name : String
getter password_hash : String
getter gid : Int32
getter users = Array(String).new
property name : String
property password_hash : String
property gid : Int32
property users = Array(String).new
def initialize(@name, @password_hash, @gid, @users = [] of String)
end

View File

@ -87,6 +87,12 @@ class Passwd
end
end
def each_group(&block)
group_as_array.each do |line|
yield Passwd::Group.new line
end
end
def get_all_users
users = Array(Passwd::User).new
@ -97,6 +103,22 @@ class Passwd
users
end
def get_group(&block) : Passwd::Group?
each_group do |group|
if yield group
return group
end
end
end
def get_group(gid : Int32)
get_group &.gid.==(gid)
end
def get_group(name : String)
get_group &.name.==(name)
end
def get_all_groups
groups = Array(Passwd::Group).new
@ -211,6 +233,20 @@ class Passwd
File.write @passwd, new_passwd.join("\n") + "\n"
end
def mod_group(group : ::Passwd::Group)
new_group = group_as_array.map do |line|
_group = Passwd::Group.new line
if _group.gid == group.gid
group.to_csv
else
line.join(':')
end
end
File.write @group, new_group.join('\n') + '\n'
end
private def safe_rewrite(path : String, body : String)
tempfile = File.tempfile(File.basename path)
tempfile << body

View File

@ -1,18 +1,18 @@
require "csv"
class Passwd::User
getter uid : Int32
getter gid : Int32
getter login : String
getter password_hash : String
getter home : String = "/"
getter shell : String = "/bin/nologin"
getter groups = Array(String).new
getter full_name : String? = nil
getter location : String? = nil
getter office_phone_number : String? = nil
getter home_phone_number : String? = nil
getter other_contact : String? = nil
property uid : Int32
property gid : Int32
property login : String
property password_hash : String
property home : String = "/"
property shell : String = "/bin/nologin"
property groups = Array(String).new
property full_name : String? = nil
property location : String? = nil
property office_phone_number : String? = nil
property home_phone_number : String? = nil
property other_contact : String? = nil
def initialize(
@login,