Class: Ayadn::Authorize

Inherits:
Object show all
Defined in:
lib/ayadn/authorize.rb

Instance Method Summary collapse

Constructor Details

#initializeAuthorize

Returns a new instance of Authorize.



5
6
7
8
# File 'lib/ayadn/authorize.rb', line 5

def initialize
  @status = Status.new # global statuses + utils
  @baseURL = "https://api.app.net" # may be overriden
end

Instance Method Details

#authorize(options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ayadn/authorize.rb', line 10

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
  # overrides the config file
  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
  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)
  @status.say_yellow :create, "configuration"
  Settings.load_config
  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(@status).list
end

#unauthorize(user, options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ayadn/authorize.rb', line 50

def unauthorize(user, options)
  begin
    if user.size != 1
      @status.one_username
      exit
    end
    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)
    else
      sure = thor.yes?("Are you sure you want to unauthorize user @#{user} ? [y/N]\n\n> ", :red)
    end
    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
      # 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.(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.canceled
    exit
  end
end