Class: Cli::UserCli

Inherits:
Base
  • Object
show all
Defined in:
lib/cli/user.rb

Instance Method Summary collapse

Instance Method Details

#addObject



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
49
50
51
52
53
54
55
56
57
58
# File 'lib/cli/user.rb', line 17

def add
  if options[:name].length > 200
    Error.raise("User name can not be longer than 200 characters", 100)
  elsif options[:login_id] != nil && options[:login_id].length > 255
    Error.raise("User login_id can not be longer than 255 characters",100)
  elsif options[:password].length > 255
    Error.raise("User password can not be longer than 255 characters", 100)
  elsif options[:primary_account_id] != nil && options[:primary_account_id].length > 255
    Error.raise(Thor::Error, "User primary_account_id can not be longer than 255 characters",100)
  else
    #Check if the primary account uuid exists
    Error.raise("Unknown Account UUID #{options[:primary_account_id]}",100) if options[:primary_account_id] != nil && [options[:primary_account_id]].nil?
    
    #The login id is needed to log into the web ui. Therefore we set it to name if it isn't provided.
    if options[:login_id].nil?
       = options[:name]
    else
       = options[:login_id]
    end
    
    #Check if username is available
    Error.raise("Login id already in use: '#{login_id}'.",100) unless User.find(:name => ).nil?
    
    #Encrypt the password
    pwd_hash = User.encrypt_password(options[:password])
    
    #Put them in there
    fields = {:name => options[:name], :login_id => , :password => pwd_hash}
    fields.merge!({:uuid => options[:uuid]}) unless options[:uuid].nil?
    new_uuid = super(User,fields)
    
    #TODO: put this in the model instead
    unless options[:primary_account_id] == nil
      new_user = User[new_uuid]
      prim_acc = [options[:primary_account_id]]
      new_user.(prim_acc)
      new_user. = prim_acc.uuid
      new_user.save
    end
    puts new_uuid
  end
end

#associate(uuid) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cli/user.rb', line 146

def associate(uuid)      
  user = User[uuid] || UnknownUUIDError.raise(uuid)
  options[:account_ids].each { |a|
    if [a].nil?
      puts "Unknown Account UUID: #{a}" if options[:verbose]
    elsif !user.accounts.index([a]).nil?
      puts "User #{uuid} is already associated with account #{a}." if options[:verbose]
    else
      user.([a])
      if user..nil?
        user. = .trim_uuid(a)
        user.save
      end
      puts "User #{uuid} successfully associated with account #{a}." if options[:verbose]
    end
  }
end

#del(uuid) ⇒ Object



123
124
125
126
# File 'lib/cli/user.rb', line 123

def del(uuid)
  super(User,uuid)
  puts "User #{uuid} has been deleted." if options[:verbose]
end

#dissociate(uuid) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/cli/user.rb', line 167

def dissociate(uuid)
  user = User[uuid] || UnknownUUIDError.raise(uuid)
  options[:account_ids].each { |a|
    if [a].nil?
      puts "Unknown Account UUID: #{a}" if options[:verbose]
    elsif user.accounts.index([a]).nil?
      puts "User #{uuid} is not associated with account #{a}." if options[:verbose]
    else
      user.([a])
      
      puts "User #{uuid} successfully dissociated from account #{a}." if options[:verbose]
      
      if [a].uuid == user.
        user. = nil
        user.save
        puts "This was user #{uuid}'s primary account. Has been set to Null now." if options[:verbose]
      end
    end
  }
end

#modify(uuid) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cli/user.rb', line 107

def modify(uuid)
  Error.raise("User name can not be longer than 200 characters",100) if options[:name] != nil && options[:name].length > 200
  Error.raise("User login_id can not be longer than 255 characters",100) if options[:login_id] != nil && options[:login_id].length > 255
  Error.raise("User password can not be longer than 255 characters",100) if options[:password] != nil && options[:password].length > 255
  Error.raise("User primary_account_id can not be longer than 255 characters",100) if options[:primary_account_id] != nil && options[:primary_account_id].length > 255
  
  fields = options.merge({})
  fields[:password] = User.encrypt_password(options[:password])
  fields[:primary_account_id] = .trim_uuid(options[:primary_account_id]) unless options[:primary_account_id].nil?
  
  super(User,uuid,fields)
end

#primacc(uuid) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cli/user.rb', line 130

def primacc(uuid)
  user = User[uuid] || UnknownUUIDError.raise(uuid)
  
  if options[:account_id]
    acc = [options[:account_id]] || UnknownUUIDError.raise(options[:account_id])
    user. = acc.uuid
    user.save
    user.(acc) unless user.accounts.member?(acc)
  else
    puts .uuid_prefix + "-" + user.
  end
end

#show(uuid = nil) ⇒ Object



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
# File 'lib/cli/user.rb', line 61

def show(uuid = nil)
  if uuid        
    user = User[uuid] || UnknownUUIDError.raise(uuid)
    puts ERB.new("User UUID:\n  <%= user.canonical_uuid %>\nName:\n  <%= user.name %>\n<%- if user.login_id -%>\nLogin ID:\n  <%= user.login_id %>\n<%- end -%>\n<%- if user.primary_account_id -%>\nPrimary Account:\n<%- prim_acc = Account.find(:uuid => user.primary_account_id) -%>\n  <%- if prim_acc.class == Account -%>\n  <%= prim_acc.canonical_uuid %>\\t<%= prim_acc.name %>\n<%- else -%>\n  <%= Account.uuid_prefix%>-<%= prim_acc.uuid %>\\t<%= prim_acc.name %>\n<%- end -%>\n<%- end -%>\n<%- unless user.accounts.empty? -%>\nAssociated accounts:\n<%- user.accounts.each { |row| -%>\n<%- if row.class == Account -%>\n  <%= row.canonical_uuid %>\\t<%= row.name %>\n<%- else -%>\n  <%= Account.uuid_prefix%>-<%= row.uuid %>\\t<%= row.name %>\n<%- end -%>\n<%- } -%>\n<%- end -%>\n", nil, '-').result(binding)
  else
    user = User.all
    puts ERB.new("<%- user.each { |row| -%>\n<%= row.canonical_uuid %>\\t<%= row.name %>\n<%- } -%>\n", nil, '-').result(binding)
  end
end