60 lines
1.2 KiB
Ruby
60 lines
1.2 KiB
Ruby
|
module EmacsCollab
|
||
|
|
||
|
class User
|
||
|
attr_accessor :id, :keypath, :pw_hash, :salt, :keys
|
||
|
|
||
|
def to_s
|
||
|
"#{@id}"
|
||
|
end
|
||
|
|
||
|
def initialize(id)
|
||
|
@id = id
|
||
|
@keypath = "#{USER_LOCATION}#{id}"
|
||
|
refresh_keys
|
||
|
end
|
||
|
|
||
|
def add_key_from_string(keystring)
|
||
|
unless valid_pubkey? keystring
|
||
|
raise InvalidSSHPubKey, "Public key not valid"
|
||
|
end
|
||
|
ks = keystring.split(" ");
|
||
|
key = SSHKey.new(ks[0][4..6], ks[1], ks[2])
|
||
|
|
||
|
unless @keys.include? key
|
||
|
@keys << key
|
||
|
else
|
||
|
raise DuplicateSSHPubKey, "Public key is already added to user"
|
||
|
end
|
||
|
|
||
|
flush
|
||
|
refresh_keys
|
||
|
flush_projects_with_user(self)
|
||
|
end
|
||
|
|
||
|
def remove_key(key)
|
||
|
@keys.delete(key)
|
||
|
flush
|
||
|
refresh_keys
|
||
|
flush_projects_with_user(self)
|
||
|
end
|
||
|
|
||
|
def refresh_keys
|
||
|
#File.open(@keypath,"r") do |f|
|
||
|
# @keys = f.read.split("\n").select{ |line| line[0..2] == "ssh" }.map{ |ks| k = ks.split(" "); SSHKey.new(k[0][4..6], k[1], k[2]) }
|
||
|
#end
|
||
|
end
|
||
|
|
||
|
def flush
|
||
|
File.open(@keypath,"w+") do |key_file|
|
||
|
@keys.each do |key|
|
||
|
key_file << "#{key.to_s}\n"
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class UserNotFoundError < StandardError
|
||
|
end
|
||
|
|
||
|
end
|