lib/ayadn/authorize.rb in ayadn-3.0 vs lib/ayadn/authorize.rb in ayadn-4.0

- old
+ new

@@ -1,21 +1,22 @@ # encoding: utf-8 module Ayadn class Authorize def initialize - @thor = Thor::Shell::Color.new # local statuses @status = Status.new # global statuses + utils @baseURL = "https://api.app.net" # may be overriden end def authorize(options) puts "\n" + # /ayadn/accounts.db is the Ayadn 1.x deprecated database if File.exist?(Dir.home + "/ayadn/accounts.db") @status.deprecated_ayadn exit end + # default config for ADN API api_file = Dir.home + "/ayadn/.api.yml" # overrides the default value if File.exist?(api_file) @baseURL = YAML.load(File.read(api_file))[:root] end @@ -23,16 +24,16 @@ if options["api"] @baseURL = options["api"] end puts "\e[H\e[2J" show_link + # get the auth token from CLI or ask user token = if options["token"] options["token"][0] else get_token end - # token = get_token check_token(token) puts "\e[H\e[2J" @status.say_yellow :connexion, "downloading user info" user = create_user_data(token, Dir.home + "/ayadn") prepare(user) @@ -41,55 +42,63 @@ Logs.create_logger install @status.say_green :done, "user #{user.handle} is authorized" Errors.info "#{user.handle} authorized." @status.say { @status.say_green :end, "Thank you for using Ayadn. Enjoy!" } - Switch.new.list + Switch.new(@status).list end def unauthorize(user, options) begin - @workers = Workers.new if user.size != 1 @status.one_username exit end - user = @workers.remove_arobase_if_present(user)[0] + user = Workers.new.remove_arobase_if_present(user)[0] puts "\e[H\e[2J" + thor = Thor::Shell::Color.new if options[:delete] - sure = @thor.yes?("Are you sure you want to unauthorize user @#{user} and delete its folders? [y/N]\n\n> ", :red) + sure = thor.yes?("Are you sure you want to unauthorize user @#{user} and delete its folders? [y/N]\n\n> ", :red) else - sure = @thor.yes?("Are you sure you want to unauthorize user @#{user} ? [y/N]\n\n> ", :red) + sure = thor.yes?("Are you sure you want to unauthorize user @#{user} ? [y/N]\n\n> ", :red) end - unless sure == true - Status.new.canceled + unless sure + @status.canceled exit end puts "\e[H\e[2J" @status.say_yellow :delete, "database entry for @#{user}" - + # load the current accounts DB db = Amalgalite::Database.new(Dir.home + "/ayadn/accounts.sqlite") - + # remember who we are + active_user = Databases.all_accounts(db).select { |acc| acc[4] == 1 }[0][0] + # remove this user from DB Databases.remove_from_accounts(db, user) if options[:delete] @status.say_yellow :delete, "@#{user} user folders" FileUtils.remove_dir(Dir.home + "/ayadn/#{user}") end @status.say_green :done, "user @#{user} has been unauthorized" remaining = Databases.all_accounts(db) if remaining.flatten.empty? @status.say_info "accounts database is now empty" else - username = remaining[0][0] + # are we still here? + if !remaining.select { |arr| arr[0] == active_user }.empty? + username = active_user + else + # just to avoid having Ayadn without any user logged in + username = remaining[0][0] + end Databases.set_active_account(db, username) @status.say_info "user @#{username} is now the active user" end puts "\n" rescue Amalgalite::SQLite3::Error => e @status.not_authorized exit rescue Interrupt - Status.new.canceled + @status.canceled exit end end private