Class: Api::V1::UsersController
- Inherits:
-
BaseController
- Object
- BaseController
- Api::V1::UsersController
- Defined in:
- app/controllers/api/v1/users_controller.rb
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #effective_security ⇒ Object
- #index ⇒ Object
- #reset_password ⇒ Object
- #update ⇒ Object
- #update_security ⇒ Object
Instance Method Details
#create ⇒ Object
44 45 46 47 48 49 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/controllers/api/v1/users_controller.rb', line 44 def create begin ActiveRecord::Base.connection.transaction do current_user.with_capability(:create, 'User') do user = User.new( :email => params[:email], :username => params[:username], :password => params[:password], :password_confirmation => params[:password_confirmation] ) # set this to tell activation where to redirect_to for login and temp password login_url = params[:login_url] || '/erp_app/login' # if a website was selected then set it so we can use the any templates in that website unless params['website_id'].blank? user.add_instance_attribute(:website_id, params['website_id']) end #set this to tell activation where to redirect_to for login and temp password user.add_instance_attribute(:login_url, login_url) user.add_instance_attribute(:temp_password, params[:password]) if params[:auto_activate] == 'yes' user.skip_activation_email = true end user.save! if params[:auto_activate] == 'yes' user.activate! end if params[:party_id] user.party = Party.find(params[:party_id]) user.save! else individual = Individual.create(:gender => params[:gender], :current_first_name => params[:first_name], :current_last_name => params[:last_name]) user.party = individual.party user.save user.party.created_by_party = current_user.party user.party.save! # add employee role to party party = individual.party party.add_role_type(RoleType.find_or_create('employee', 'Employee')) # associate the new party to the dba_organization of the user creating this user relationship_type = RelationshipType.find_or_create(RoleType.find_or_create('dba_org', 'Doing Business As Organization'), RoleType.find_or_create('employee', 'Employee')) party.create_relationship(relationship_type.description, current_user.party.dba_organization.id, relationship_type) end render :json => {:success => true, user: user.to_data_hash} end end rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex render :json => {:success => false, :message => ex., :user => nil} rescue ActiveRecord::RecordInvalid => invalid Rails.logger.error invalid.record.errors = "<ul>" invalid.record.errors.collect do |e, m| << "<li>#{e} #{m}</li>" end << "</ul>" render :json => {:success => false, :message => , :user => nil} rescue StandardError => ex Rails.logger.error ex. Rails.logger.error ex.backtrace.join("\n") ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier render :json => {:success => false, :message => 'Error creating user', :user => nil} end end |
#destroy ⇒ Object
214 215 216 217 218 219 220 221 222 |
# File 'app/controllers/api/v1/users_controller.rb', line 214 def destroy user = User.find(params[:id]) # get the party as it will also destroy the user party = user.party party.destroy render :json => {:success => true} end |
#effective_security ⇒ Object
224 225 226 227 228 |
# File 'app/controllers/api/v1/users_controller.rb', line 224 def effective_security user = User.find(params[:id]) render :json => {:success => true, :capabilities => user.class_capabilities_to_hash} end |
#index ⇒ Object
5 6 7 8 9 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 |
# File 'app/controllers/api/v1/users_controller.rb', line 5 def index username = params[:username] sort_hash = params[:sort].blank? ? {} : Hash.symbolize_keys(JSON.parse(params[:sort]).first) sort = sort_hash[:property] || 'username' dir = sort_hash[:direction] || 'ASC' limit = params[:limit] || 25 start = params[:start] || 0 # scope users by dba_organization and any of its children dba_orgs dba_organization = current_user.party.dba_organization dba_org_ids = dba_organization.child_dba_organizations.collect(&:id) dba_org_ids.push(dba_organization.id) dba_org_ids.uniq! users = User.joins(:party).joins("inner join party_relationships as dba_reln on (dba_reln.party_id_from = parties.id and dba_reln.party_id_to in (#{dba_org_ids.join(',')}) and dba_reln.role_type_id_to = #{RoleType.iid('dba_org').id} )") # TODO update for more advance searching if params[:query_filter].present? username = params[:query_filter].strip end if username.blank? total_count = users.uniq.count users = users.order("#{sort} #{dir}").offset(start).limit(limit) else users = users.where('username like ? or email like ?', "%#{username}%", "%#{username}%") total_count = users.uniq.count users = users.order("#{sort} #{dir}").offset(start).limit(limit) end render :json => {total_count: total_count, users: users.uniq.collect(&:to_data_hash)} end |
#reset_password ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'app/controllers/api/v1/users_controller.rb', line 194 def reset_password begin user = User.find(params[:id]) user.add_instance_attribute(:reset_password_url, (params[:reset_password_url] || '/erp_app/reset_password')) user.add_instance_attribute(:domain, params[:domain]) user.deliver_reset_password_instructions! = "Password has been reset. An email has been sent with further instructions to #{user.email}." success = true render :json => {:success => success, :message => } rescue => ex Rails.logger.error ex. Rails.logger.error ex.backtrace.join("\n") ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier render :json => {:success => false, :message => 'Could not reset password'} end end |
#update ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'app/controllers/api/v1/users_controller.rb', line 127 def update begin ActiveRecord::Base.transaction do if params[:id] user = User.find(params[:id]) party = user.party else user = current_user party = user.party end if params[:password].present? user.password = params[:password].strip if params[:password_confirmation].present? user.password_confirmation = params[:password_confirmation].strip else user.password_confirmation = params[:password].strip end end if params[:username].present? user.username = params[:username].strip end if params[:status] user.activation_state = params[:status] end if params[:email].present? user.email = params[:email].strip end user.save! business_party = party.business_party # update business party information if params[:first_name].present? business_party.current_first_name = params[:first_name].strip end if params[:last_name].present? business_party.current_last_name = params[:last_name].strip end user.party.updated_by_party = current_user.party user.party.save! render :json => {:success => true, :message => 'User updated', :user => user.to_data_hash} end rescue ActiveRecord::RecordInvalid => invalid Rails.logger.error invalid.record.errors render :json => {:success => false, :message => invalid.record.errors., :user => nil} rescue StandardError => ex Rails.logger.error ex. Rails.logger.error ex.backtrace.join("\n") ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier render :json => {:success => false, :message => 'Error updating user', :user => nil} end end |
#update_security ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'app/controllers/api/v1/users_controller.rb', line 230 def update_security begin ActiveRecord::Base.transaction do user = User.find(params[:id]) user.remove_all_security_roles user.add_security_roles(params[:security_role_iids].split(',')) user.remove_all_groups user.add_groups(params[:group_ids].split(',').map{|group_id| Group.find(group_id)}) user.remove_all_capabilities user.add_capabilities(params[:capability_ids].split(',').map{|capability_id| Capability.find(capability_id)}) render json: {success: true} end rescue ActiveRecord::RecordInvalid => invalid Rails.logger.error invalid.record.errors render :json => {:success => false, :message => invalid.record.errors., :user => nil} rescue StandardError => ex Rails.logger.error ex. Rails.logger.error ex.backtrace.join("\n") ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier render :json => {:success => false, :message => 'Error updating security', :user => nil} end end |