Class: Cli::UserCli
Instance Method Summary collapse
- #add ⇒ Object
- #associate(uuid) ⇒ Object
- #del(uuid) ⇒ Object
- #dissociate(uuid) ⇒ Object
- #modify(uuid) ⇒ Object
- #primacc(uuid) ⇒ Object
- #show(uuid = nil) ⇒ Object
Instance Method Details
#add ⇒ Object
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 [:name].length > 200 Error.raise("User name can not be longer than 200 characters", 100) elsif [:login_id] != nil && [:login_id].length > 255 Error.raise("User login_id can not be longer than 255 characters",100) elsif [:password].length > 255 Error.raise("User password can not be longer than 255 characters", 100) elsif [:primary_account_id] != nil && [: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 [:primary_account_id] != nil && Account[[: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 [:login_id].nil? login_id = [:name] else login_id = [:login_id] end #Check if username is available Error.raise("Login id already in use: '#{login_id}'.",100) unless User.find(:name => login_id).nil? #Encrypt the password pwd_hash = User.encrypt_password([:password]) #Put them in there fields = {:name => [:name], :login_id => login_id, :password => pwd_hash} fields.merge!({:uuid => [:uuid]}) unless [:uuid].nil? new_uuid = super(User,fields) #TODO: put this in the model instead unless [:primary_account_id] == nil new_user = User[new_uuid] prim_acc = Account[[:primary_account_id]] new_user.add_account(prim_acc) new_user.primary_account_id = 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) [:account_ids].each { |a| if Account[a].nil? puts "Unknown Account UUID: #{a}" if [:verbose] elsif !user.accounts.index(Account[a]).nil? puts "User #{uuid} is already associated with account #{a}." if [:verbose] else user.add_account(Account[a]) if user.primary_account_id.nil? user.primary_account_id = Account.trim_uuid(a) user.save end puts "User #{uuid} successfully associated with account #{a}." if [: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 [: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) [:account_ids].each { |a| if Account[a].nil? puts "Unknown Account UUID: #{a}" if [:verbose] elsif user.accounts.index(Account[a]).nil? puts "User #{uuid} is not associated with account #{a}." if [:verbose] else user.remove_account(Account[a]) puts "User #{uuid} successfully dissociated from account #{a}." if [:verbose] if Account[a].uuid == user.primary_account_id user.primary_account_id = nil user.save puts "This was user #{uuid}'s primary account. Has been set to Null now." if [: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 [:name] != nil && [:name].length > 200 Error.raise("User login_id can not be longer than 255 characters",100) if [:login_id] != nil && [:login_id].length > 255 Error.raise("User password can not be longer than 255 characters",100) if [:password] != nil && [:password].length > 255 Error.raise("User primary_account_id can not be longer than 255 characters",100) if [:primary_account_id] != nil && [:primary_account_id].length > 255 fields = .merge({}) fields[:password] = User.encrypt_password([:password]) fields[:primary_account_id] = Account.trim_uuid([:primary_account_id]) unless [: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 [:account_id] acc = Account[[:account_id]] || UnknownUUIDError.raise([:account_id]) user.primary_account_id = acc.uuid user.save user.add_account(acc) unless user.accounts.member?(acc) else puts Account.uuid_prefix + "-" + user.primary_account_id 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 |