passwd.cr/src/user.cr

69 lines
1.5 KiB
Crystal

require "csv"
class Passwd::User
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,
@password_hash,
@uid,
@gid,
@home = "",
@shell = "",
@full_name = nil,
@location = nil,
@office_phone_number = nil,
@home_phone_number = nil,
@other_contact = nil
)
end
# Caution: will raise on invalid entries.
def initialize(line : Array(String))
@login = line[0]
@password_hash = line[1]
@uid = line[2].to_i
@gid = line[3].to_i
CSV.parse(line[4], separator: ',')[0]?.try do |gecos|
@full_name = gecos[0]?
@location = gecos[1]?
@office_phone_number = gecos[2]?
@home_phone_number = gecos[3]?
@other_contact = gecos[4]?
end
@home = line[5]
@shell = line[6]
end
def to_csv
[@login, @password_hash, @uid, @gid, gecos, @home, @shell].join ":"
end
def gecos
unless @location || @office_phone_number || @home_phone_number || @other_contact
if @full_name
return @full_name
else
return ""
end
end
[@full_name || "", @location || "", @office_phone_number || "", @home_phone_number || "", @other_contact || ""].join ","
end
end