code cleanup
This commit is contained in:
parent
291aeb98ed
commit
2c2a29906b
49
collab.rb
49
collab.rb
|
@ -104,7 +104,6 @@ class Project
|
|||
def add_user(user)
|
||||
@users << user
|
||||
user.keys.each do |key|
|
||||
p key
|
||||
add_key(key)
|
||||
end
|
||||
flush
|
||||
|
@ -119,9 +118,8 @@ class Project
|
|||
|
||||
def refresh
|
||||
@users = File.open("#{@path}/.ssh/users","r") do |f|
|
||||
f.read.split("\n").map{|id| p id; puts $user_id_map; get_user_by_id(id)}
|
||||
f.read.split("\n").map{|id| get_user_by_id(id)}
|
||||
end
|
||||
p @users
|
||||
@keys = extract_ssh_pubkeys("#{@path}/.ssh/authorized_keys")
|
||||
end
|
||||
|
||||
|
@ -143,6 +141,9 @@ class Project
|
|||
|
||||
end
|
||||
|
||||
class DuplicateProjectError < StandardError
|
||||
end
|
||||
|
||||
def get_project(id)
|
||||
$project_id_map[id]
|
||||
end
|
||||
|
@ -153,10 +154,16 @@ def add_project(project)
|
|||
end
|
||||
|
||||
def create_project(id)
|
||||
if not system("useradd -m #{POSIX_NAME_PREFIX}#{id}") then
|
||||
raise "Project creation failed"
|
||||
$projects.each do |p|
|
||||
if p == id then
|
||||
raise DuplicateProjectError, "Project with id #{id} already exists"
|
||||
end
|
||||
end
|
||||
|
||||
if not system("useradd -m #{POSIX_NAME_PREFIX}#{id} -s /usr/bin/eshell") then
|
||||
raise "Project creation failed"
|
||||
end
|
||||
|
||||
project = Project.new
|
||||
project.id = id
|
||||
project.path = "/home/#{POSIX_NAME_PREFIX}#{id}"
|
||||
|
@ -167,14 +174,14 @@ def create_project(id)
|
|||
FileUtils.mkdir "#{project.path}/.ssh"
|
||||
FileUtils.touch "#{project.path}/.ssh/authorized_keys"
|
||||
FileUtils.touch "#{project.path}/.ssh/users"
|
||||
FileUtils.chown_R project.id, project.id, "#{project.path}/.ssh"
|
||||
FileUtils.chown_R project.posixname, project.posixname, "#{project.path}/.ssh"
|
||||
|
||||
project
|
||||
end
|
||||
|
||||
def remove_project(project)
|
||||
system("userdel -r #{project.posixname}")
|
||||
$projects.remove(project)
|
||||
$projects.delete(project)
|
||||
$project_id_map[id] = nil
|
||||
end
|
||||
|
||||
|
@ -234,34 +241,6 @@ def get_ssh_pubkeys(project)
|
|||
extract_ssh_pubkeys("#{project.path}/.ssh/authorized_keys")
|
||||
end
|
||||
|
||||
# Deprecate
|
||||
def add_ssh_pubkey(project,key)
|
||||
unless valid_pubkey? key
|
||||
raise InvalidSSHPubKey, "Public key not valid"
|
||||
end
|
||||
get_ssh_keyfile(project,"a") do |key_file|
|
||||
key_file << "#{key.to_s}\n"
|
||||
end
|
||||
end
|
||||
|
||||
def reset_pubkeys(project)
|
||||
FileUtils.rm "#{project.path}/.ssh/authorized_keys"
|
||||
FileUtils.touch "#{project.path}/.ssh/authorized_keys"
|
||||
FileUtils.chown_R project.id, project.id, "#{project.path}/.ssh"
|
||||
end
|
||||
|
||||
def remove_ssh_pubkey(project,key)
|
||||
keys = get_ssh_pubkeys(project)
|
||||
reset_pubkeys(project)
|
||||
keys.each{ |k| unless k == key then add_ssh_pubkey(project,k) end}
|
||||
end
|
||||
|
||||
def remove_ssh_pubkey_by_key
|
||||
end
|
||||
|
||||
def remove_ssh_pubkey_by_comment
|
||||
end
|
||||
|
||||
def valid_pubkey?(key)
|
||||
IO.popen("ssh-keygen -qlf -","r+") do |io|
|
||||
io.write key.to_s
|
||||
|
|
45
prompt.rb
45
prompt.rb
|
@ -51,13 +51,37 @@ def keys_user(user)
|
|||
end
|
||||
|
||||
def projects
|
||||
choices = $projects
|
||||
project = $prompt.select("Pick a project", choices)
|
||||
keys_project(project)
|
||||
choices = choices = {'Create new project' => :create,
|
||||
'Modify existing project' => :modify}
|
||||
case $prompt.select("What do you want to do?", choices)
|
||||
when :create
|
||||
project_create
|
||||
when :modify
|
||||
project_modify
|
||||
end
|
||||
end
|
||||
|
||||
def keys_project(project)
|
||||
choices = {'List users' => :list,
|
||||
def project_create
|
||||
id = $prompt.ask("What should be the id of the new project?") do |input|
|
||||
input.required true
|
||||
input.validate /\A\w+\Z/
|
||||
end
|
||||
begin
|
||||
create_project(id)
|
||||
rescue DuplicateProjectError => e
|
||||
puts e.message.colorize(:red)
|
||||
projects
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def project_modify
|
||||
# Currently, it is implied that modify equals changing which users have access
|
||||
project_users($prompt.select("Pick a project", $projects))
|
||||
end
|
||||
|
||||
def project_users(project)
|
||||
choices = {'List users with access' => :list,
|
||||
'Add user' => :add,
|
||||
'Select users with access' => :select}
|
||||
case $prompt.select("What do you want to do?", choices)
|
||||
|
@ -70,16 +94,23 @@ def keys_project(project)
|
|||
puts "Added user #{user} to #{project}".colorize(:green)
|
||||
when :select
|
||||
choices = $users
|
||||
|
||||
# Mark already added users as "default"
|
||||
counter = 1
|
||||
defaults = []
|
||||
counter = 1
|
||||
choices.each do |u|
|
||||
if project.users.include? u then
|
||||
defaults << counter
|
||||
end
|
||||
counter += 1
|
||||
end
|
||||
users = $prompt.multi_select("Select users", choices, default: defaults)
|
||||
|
||||
if defaults != []
|
||||
users = $prompt.multi_select("Select users", choices, default: defaults)
|
||||
else
|
||||
users = $prompt.multi_select("Select users", choices)
|
||||
end
|
||||
|
||||
project.users = users
|
||||
project.flush
|
||||
project.refresh
|
||||
|
|
Loading…
Reference in New Issue
Block a user