Added group edition APIs.
Still incomplete, still WIP, but it’s a start.
This commit is contained in:
parent
4a65a41f16
commit
b9568fb33a
@ -1,10 +1,10 @@
|
|||||||
require "csv"
|
require "csv"
|
||||||
|
|
||||||
class Passwd::Group
|
class Passwd::Group
|
||||||
getter name : String
|
property name : String
|
||||||
getter password_hash : String
|
property password_hash : String
|
||||||
getter gid : Int32
|
property gid : Int32
|
||||||
getter users = Array(String).new
|
property users = Array(String).new
|
||||||
|
|
||||||
def initialize(@name, @password_hash, @gid, @users = [] of String)
|
def initialize(@name, @password_hash, @gid, @users = [] of String)
|
||||||
end
|
end
|
||||||
|
@ -87,6 +87,12 @@ class Passwd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def each_group(&block)
|
||||||
|
group_as_array.each do |line|
|
||||||
|
yield Passwd::Group.new line
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def get_all_users
|
def get_all_users
|
||||||
users = Array(Passwd::User).new
|
users = Array(Passwd::User).new
|
||||||
|
|
||||||
@ -97,6 +103,22 @@ class Passwd
|
|||||||
users
|
users
|
||||||
end
|
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
|
def get_all_groups
|
||||||
groups = Array(Passwd::Group).new
|
groups = Array(Passwd::Group).new
|
||||||
|
|
||||||
@ -211,6 +233,20 @@ class Passwd
|
|||||||
File.write @passwd, new_passwd.join("\n") + "\n"
|
File.write @passwd, new_passwd.join("\n") + "\n"
|
||||||
end
|
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)
|
private def safe_rewrite(path : String, body : String)
|
||||||
tempfile = File.tempfile(File.basename path)
|
tempfile = File.tempfile(File.basename path)
|
||||||
tempfile << body
|
tempfile << body
|
||||||
|
24
src/user.cr
24
src/user.cr
@ -1,18 +1,18 @@
|
|||||||
require "csv"
|
require "csv"
|
||||||
|
|
||||||
class Passwd::User
|
class Passwd::User
|
||||||
getter uid : Int32
|
property uid : Int32
|
||||||
getter gid : Int32
|
property gid : Int32
|
||||||
getter login : String
|
property login : String
|
||||||
getter password_hash : String
|
property password_hash : String
|
||||||
getter home : String = "/"
|
property home : String = "/"
|
||||||
getter shell : String = "/bin/nologin"
|
property shell : String = "/bin/nologin"
|
||||||
getter groups = Array(String).new
|
property groups = Array(String).new
|
||||||
getter full_name : String? = nil
|
property full_name : String? = nil
|
||||||
getter location : String? = nil
|
property location : String? = nil
|
||||||
getter office_phone_number : String? = nil
|
property office_phone_number : String? = nil
|
||||||
getter home_phone_number : String? = nil
|
property home_phone_number : String? = nil
|
||||||
getter other_contact : String? = nil
|
property other_contact : String? = nil
|
||||||
|
|
||||||
def initialize(
|
def initialize(
|
||||||
@login,
|
@login,
|
||||||
|
Loading…
Reference in New Issue
Block a user