ftp-manager/app/services/ftp_config.rb
2016-10-09 20:58:30 -05:00

46 lines
1.0 KiB
Ruby

# frozen_string_literal: true
class FtpConfig
def build_password_list accounts
`sudo chown $(whoami) #{password_file}`
File.open(password_file, "w+") do |file|
Array(accounts).each do |account|
file.write "#{account.username}:#{hash_password(account.password)}"
end
end
`sudo chown root #{password_file}`
end
def build_user_configs accounts
`sudo chown -R $(whoami) #{config_path}`
Array(accounts).each do |account|
File.open("#{config_path}#{account.username}", "w+") do |file|
`sudo -u #{ftp_user} mkdir -p #{ftp_root}#{account.home_folder}`
file.write "local_root=#{ftp_root}#{account.home_folder}"
end
end
`sudo chown -R root #{config_path}`
end
private
def password_file
AppConfig.htpasswd
end
def config_path
AppConfig.ftpusers
end
def ftp_root
AppConfig.ftproot
end
def ftp_user
AppConfig.ftpaccount
end
def hash_password password
`openssl passwd -1 -noverify -quiet #{password}`
end
end