Changeset 707
- Timestamp:
- 10/11/07 20:17:12 (1 year ago)
- Files:
-
- trunk/app/controllers/application.rb (modified) (5 diffs)
- trunk/app/controllers/asset_controller.rb (modified) (3 diffs)
- trunk/app/controllers/asset_permission_controller.rb (modified) (1 diff)
- trunk/app/controllers/task_type_controller.rb (modified) (2 diffs)
- trunk/app/controllers/timeline_controller.rb (modified) (1 diff)
- trunk/app/controllers/user_controller.rb (modified) (1 diff)
- trunk/app/models/asset.rb (modified) (12 diffs)
- trunk/app/models/cmms_base.rb (modified) (1 diff)
- trunk/app/models/lockable.rb (modified) (3 diffs)
- trunk/app/models/message.rb (modified) (1 diff)
- trunk/app/models/protected_asset_data.rb (modified) (1 diff)
- trunk/app/models/protected_data.rb (modified) (6 diffs)
- trunk/app/models/protector_model.rb (modified) (2 diffs)
- trunk/app/models/task.rb (modified) (3 diffs)
- trunk/app/models/task_type.rb (modified) (4 diffs)
- trunk/app/models/timeline_event.rb (modified) (1 diff)
- trunk/app/models/user.rb (modified) (8 diffs)
- trunk/app/views/asset/_view_table.rhtml (modified) (1 diff)
- trunk/app/views/user/edit.rhtml (modified) (1 diff)
- trunk/app/views/user/reset_password.rhtml (modified) (1 diff)
- trunk/config/active_record_patch.rb (modified) (5 diffs)
- trunk/config/gettext_hack.rb (modified) (2 diffs)
- trunk/config/menu.rb (modified) (1 diff)
- trunk/config/norfello_extensions.rb (modified) (1 diff)
- trunk/doc/manual/latex/tex/application_section.tex (modified) (1 diff)
- trunk/doc/manual/latex/tex/configuration_section.tex (modified) (1 diff)
- trunk/lib/login_system.rb (modified) (4 diffs)
- trunk/po/fi_FI/norfello_cmms.po (modified) (42 diffs)
- trunk/test/functional/asset_controller_test.rb (modified) (8 diffs)
- trunk/test/functional/asset_field_controller_test.rb (modified) (1 diff)
- trunk/test/functional/asset_permission_controller_test.rb (modified) (1 diff)
- trunk/test/functional/asset_type_controller_test.rb (modified) (1 diff)
- trunk/test/functional/asset_type_icon_controller_test.rb (modified) (1 diff)
- trunk/test/functional/search_controller_test.rb (modified) (2 diffs)
- trunk/test/functional/user_controller_test.rb (modified) (6 diffs)
- trunk/test/test_helper.rb (modified) (7 diffs)
- trunk/test/unit/asset_test.rb (modified) (20 diffs)
- trunk/test/unit/asset_type_test.rb (modified) (1 diff)
- trunk/test/unit/assets_user_group_test.rb (modified) (2 diffs)
- trunk/test/unit/attachment_test.rb (modified) (1 diff)
- trunk/test/unit/data_permission_profile_test.rb (modified) (2 diffs)
- trunk/test/unit/data_permission_test.rb (modified) (1 diff)
- trunk/test/unit/form_field_test.rb (modified) (1 diff)
- trunk/test/unit/form_template_test.rb (modified) (2 diffs)
- trunk/test/unit/form_test.rb (modified) (1 diff)
- trunk/test/unit/form_type_test.rb (modified) (1 diff)
- trunk/test/unit/message_test.rb (modified) (1 diff)
- trunk/test/unit/notification_mailer_test.rb (modified) (1 diff)
- trunk/test/unit/protected_asset_data_test.rb (modified) (2 diffs)
- trunk/test/unit/protected_asset_type_data_test.rb (modified) (2 diffs)
- trunk/test/unit/protected_data_test.rb (modified) (18 diffs)
- trunk/test/unit/task_template_test.rb (modified) (3 diffs)
- trunk/test/unit/task_test.rb (modified) (6 diffs)
- trunk/test/unit/task_type_test.rb (modified) (2 diffs)
- trunk/test/unit/timeline_event_test.rb (modified) (3 diffs)
- trunk/test/unit/type_attachment_test.rb (modified) (1 diff)
- trunk/test/unit/user_test.rb (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/app/controllers/application.rb
r701 r707 203 203 end 204 204 205 User .current_user = session[:user]205 UserAccount.current_user = session[:user] 206 206 login = session[:user].login if session[:user] 207 207 ActiveRecord::Base.logged_information(login, request.remote_ip) … … 299 299 # redirect - A dictionary specifying the url to which the client should 300 300 # be redirected, if the object is created succesfully. 301 # partial_to_render - The partial view to render. 301 302 # 302 303 def common_create(model_class, name_attribute='name', … … 331 332 # name_attribute - The name of the attribute of the model instance that returns 332 333 # the instance's name. 334 # redirect - A dictionary specifying the url to which the client should 335 # be redirected, if the object is created succesfully. 336 # cancel_url_options - A dictionary specifying the url to which the client should 337 # be redirected, if he chooses to cancel editing. 338 # partial_to_render - The partial view to render. 339 # partial_locals - Local variables for rendered partial view. 340 # 333 341 def common_edit(model_class, name_attribute='name', 334 342 redirect={:action => 'index'}, 335 343 cancel_url_options={:action => 'index'}, 336 partial_to_render='shared/create_or_edit') 344 partial_to_render='shared/create_or_edit', 345 partial_locals={}) 337 346 # Find model unless it's already found 338 347 @model ||= model_class.find(params[:id]) … … 348 357 end 349 358 350 render(:partial => partial_to_render, :layout => 'mainlevel', :locals => {:cancel_url_options => cancel_url_options}) 359 render(:partial => partial_to_render, :layout => 'mainlevel', 360 :locals => partial_locals.merge(:cancel_url_options => cancel_url_options)) 351 361 end 352 362 … … 396 406 397 407 flash[:notice] = _('Removed %s', model_class.n_objects(num_instances_deleted)) if num_instances_deleted > 0 408 end 409 410 # Method: common_reset_password 411 # ============================= 412 # Contains common code for all password resetting actions of different 413 # controllers. The name of the actions has to be reset_password. 414 # 415 # Parameters: 416 # ----------- 417 # model_class - The class object of the model whose instance's password 418 # should be reseted. 419 # redirect - A dictionary specifying the url to which the client should 420 # be redirected when password is resetted or resetting 421 # is cancelled. 422 # 423 def common_reset_password(model_class, redirect={:action => 'edit'}) 424 @model = model_class.find(params[:id]) 425 @title = _('Reset password for %s %s', model_class.model_name, @model.login) 426 @redirect = redirect.merge(:id => @model.id) 427 428 if request.post? 429 # Extract password from input - we don't want anything else 430 if params[:model] and @model.update_attributes(:password => params[:model][:password], :password_confirmation => params[:model][:password_confirmation]) 431 flash[:notice] = _('Password reseted') 432 info('Password reseted for %s %s' % [model_class.model_name, @model.login]) 433 redirect_to(@redirect) 434 else 435 flash[:error] = _('Unable to reset password') 436 render(:template => 'shared/reset_password') 437 end 438 else 439 render(:template => 'shared/reset_password') 440 end 398 441 end 399 442 trunk/app/controllers/asset_controller.rb
r702 r707 135 135 end 136 136 137 customer_choices = customer_choices_for_select 138 137 139 @fields = [ 138 140 [_('Code'), :text_field_with_prefix, 'code', { :size => 16, :maxlength => 10, :id => 'selected_asset_code', :prefix => @selected_asset.full_code + '-' } ], 139 [_('Name'), :text_field, 'name', { :size => 40, :maxlength => 40 } ], 140 [:inputs_for_asset_fields], 141 [_('Description'), :markup_text_area, 'description'] 141 [_('Name'), :text_field, 'name', { :size => 40, :maxlength => 40 } ] 142 142 ] 143 @fields.push([_('Customer'), :select, 'customer_id', customer_choices, { :include_blank => true }]) if customer_choices 144 @fields.push([:inputs_for_asset_fields], 145 [_('Description'), :markup_text_area, 'description']) 146 143 147 @post_params = { :id => params[:id] } 144 148 render(:partial => 'shared/create_or_edit', :layout => 'mainlevel', :locals => {:cancel_url_options => {:action => 'view' }}) … … 238 242 end 239 243 244 customer_choices = customer_choices_for_select 245 240 246 @fields = [ 241 247 [_('Code'), :text_field_with_prefix, 'code', { :size => 16, :maxlength => 10, :id => 'selected_asset_code', :prefix => @model.code_prefix } ], 242 [_('Name'), :text_field, 'name', { :size => 40, :maxlength => 40 } ], 243 [:inputs_for_asset_fields], 244 [_('Description'), :markup_text_area, 'description'] 248 [_('Name'), :text_field, 'name', { :size => 40, :maxlength => 40 } ] 245 249 ] 250 @fields.push([_('Customer'), :select, 'customer_id', customer_choices, { :include_blank => true }]) if customer_choices 251 @fields.push([:inputs_for_asset_fields], 252 [_('Description'), :markup_text_area, 'description']) 246 253 247 254 render(:partial => 'shared/create_or_edit', :layout => 'mainlevel', :locals => {:cancel_url_options => {:action => 'view'}}) … … 368 375 end 369 376 377 def customer_choices_for_select 378 choices = Customer.find(:all, :order => 'full_name').map { |c| [c.name, c.id] } 379 return nil if choices.empty? 380 return choices 381 end 370 382 end 371 383 trunk/app/controllers/asset_permission_controller.rb
r701 r707 271 271 return if request.get? and action_name == 'list' 272 272 273 return if @selected_asset.lock_user == User .current_user273 return if @selected_asset.lock_user == UserAccount.current_user 274 274 275 275 unless @selected_asset.lock_for_current_user(LOCK_DURATION) trunk/app/controllers/task_type_controller.rb
r705 r707 18 18 @title = _('Task types') 19 19 @fields = [ [_('Name'), :name], 20 [_('Notification address'), :notification_email], 20 [_('Notification address'), :notification_email], 21 21 [_('Description'), :description], 22 22 [_('Template'), :task_template], … … 63 63 def remove 64 64 common_remove(TaskType, 'name', :action => 'list') { |instance| 65 unless instance.tasks.empty? 65 if instance.is_service_request_type 66 _("\"%s\" is the service request type, so it can't be removed.", instance.name.capitalize) 67 elsif not instance.tasks.empty? 66 68 msg_could_not_remove_because_not_empty(instance) 67 69 end trunk/app/controllers/timeline_controller.rb
r488 r707 47 47 next if ids.empty? 48 48 49 id_conditions.push('(t ype = ? AND' + klass.to_s.underscore + '_id IN (?))')49 id_conditions.push('(timeline_events.type = ? AND timeline_events.' + klass.to_s.underscore + '_id IN (?))') 50 50 event_conditions.push(klass.to_s + 'Event', ids) 51 51 end trunk/app/controllers/user_controller.rb
r701 r707 178 178 end 179 179 180 # Method: reset_password 181 # ====================== 182 # Resets password for a user 183 # 180 184 def reset_password 181 @user = User.find params[:id] 182 @title = _('Reset password for user account %s', @user.login) 183 184 if request.post? 185 @user.attributes = params[:user] 186 if @user.save 187 flash[:notice] = _('Password reseted') 188 info "Password reseted for user #{@user.login}" 189 redirect_to :action => 'edit', :id => @user.id 190 else 191 flash[:error] = _('Unable to reset password') 192 render :action => 'reset_password' 193 end 194 end 195 end 196 185 common_reset_password(User, :action => 'edit') 186 end 187 188 # Method: login 189 # ============= 190 # Authenticates a user or a customer against existing user accounts. 191 # and logs her in 197 192 def login 198 193 if request.post? 199 if user = User.authenticate(params[:user_login], params[:user_password]) 200 201 # Check that user has some keys 202 if user.keyrings.empty? 203 error "Login failed: user #{params[:user_login]} doesn't have any keyrings." 204 flash.now['error'] = _('Login failed because you don\'t have any keyrings.') 205 @login = params[:user_login] 206 render(:layout => false) 194 if user = UserAccount.authenticate(params[:user_login], params[:user_password]) 195 if user.is_a?(User) 196 # Check that user has some keys 197 if user.keyrings.empty? 198 error "Login failed: user #{params[:user_login]} doesn't have any keyrings." 199 flash.now['error'] = _('Login failed because you don\'t have any keyrings.') 200 @login = params[:user_login] 201 render(:layout => false) 202 return 203 end 204 205 # Check that user has some user groups 206 if user.user_groups.empty? 207 error "Login failed: user #{params[:user_login]} doesn't belong to any user group." 208 flash.now['error'] = _('Login failed because you don\'t belong to any user group.') 209 @login = params[:user_login] 210 render(:layout => false) 211 return 212 end 213 214 # Check that user is authorized to read at least one asset 215 # Note that assocations between user groups and assets imply read 216 # permission. 217 if (user.user_groups.collect { |ug| ug.assets }.flatten).empty? 218 error "Login failed: user #{params[:user_login]} is unauthorized to read any assets." 219 flash.now['error'] = _('Login failed because you are not authorized to read any assets.') 220 @login = params[:user_login] 221 render(:layout => false) 222 return 223 end 224 225 redirect_back_or_default :controller => 'asset', :action => "view" 226 227 elsif user.is_a?(Customer) 228 # Check that customer is authorized to read at least one asset. 229 if user.assets.empty? 230 error "Login failed: customer #{params[:user_login]} is unauthorized to read any assets." 231 flash.now['error'] = _('Login failed because you are not authorized to read any assets.') 232 @login = params[:user_login] 233 render(:layout => false) 234 return 235 end 236 237 # FIXME: Remove the following two lines when the customer interface is ready to be used 238 render(:text => 'Customer login is NOT implemented yet!') 207 239 return 240 redirect_to(controller => 'customer_interface', :action => 'index') 241 else 242 # This should never happen 243 error "Unexpected user account: #{user.inspect}" 244 raise "Unexpected user account type: #{user.class}" 208 245 end 209 246 210 # Check that user has some user groups 211 if user.user_groups.empty? 212 error "Login failed: user #{params[:user_login]} doesn't belong to any user group." 213 flash.now['error'] = _('Login failed because you don\'t belong to any user group.') 214 @login = params[:user_login] 215 render(:layout => false) 216 return 217 end 218 219 # Check that user is authorized to read at least one asset 220 # Note that assocations between user groups and assets imply read 221 # permission. 222 if (user.user_groups.collect { |ug| ug.assets }.flatten).empty? 223 error "Login failed: user #{params[:user_login]} is unauthorized to read any assets." 224 flash.now['error'] = _('Login failed because you are not authorized to read any assets.') 225 @login = params[:user_login] 226 render(:layout => false) 227 return 228 end 229 230 session[:user] = user 247 # Login succesful 248 session[:user] = UserAccount.accessing(user) 231 249 session[:user_ip] = request.remote_ip 232 250 session[:user_timeout] = 2.hours.from_now 233 251 session[:show_asset_tree] = true 234 info "User #{params[:user_login]} logged in." 235 # Update access timestamp: 236 session[:user] = User.accessing(session[:user]) 252 info "#{session[:user].class} #{session[:user].login} logged in." 237 253 238 254 expire_fragment(:controller => 'asset_tree', :action => 'asset_tree', :action_suffix => session[:user].login) 239 redirect_back_or_default :controller => 'asset', :action => "view"240 255 else 241 256 error "Login failed for user #{params[:user_login]}." trunk/app/models/asset.rb
r702 r707 7 7 class Asset < ProtectorModel 8 8 # Protection 9 attr_accessible :code, :name, :description, :asset_type_id 9 attr_accessible :code, :name, :description, :asset_type_id, :customer_id 10 10 # Validation 11 11 validates_presence_of :name, :code … … 24 24 # Associations 25 25 belongs_to :asset_type 26 belongs_to :customer 26 27 has_many :forms, :dependent => :destroy 27 28 has_many :tasks, :dependent => :destroy … … 73 74 def Asset.get_first_authorized_to_read 74 75 first = Asset.find_authorized_to_read(:first, :order => 'id') 75 raise(ActiveRecord::RecordNotFound, " User #{User.current_user.login_and_name} is unauthorized to read any assets.") if first.nil?76 raise(ActiveRecord::RecordNotFound, "#{UserAccount.current_user.class} #{UserAccount.current_user.login_and_name} is unauthorized to read any assets.") if first.nil? 76 77 return first 77 78 end … … 94 95 end 95 96 97 # Overwrite the implementation in the parent class 98 def Asset.find_authorized_to_read(*args) 99 return super unless UserAccount.current_user.class == Customer 100 101 args = add_conditions_to_find_arguments(args, "#{self.table_name}.customer_id = ?", [UserAccount.current_user.id]) 102 return self.find(*args) 103 end 104 96 105 # Function: Asset.find_authorized_to 97 106 # ================================== … … 106 115 # 107 116 def Asset.find_authorized_to(operation, args) 108 return records_not_found(args) unless User .current_user109 110 group_ids = User .current_user.user_groups.map {|g| g.id }117 return records_not_found(args) unless UserAccount.current_user and UserAccount.current_user.class == User 118 119 group_ids = UserAccount.current_user.user_groups.map {|g| g.id } 111 120 return records_not_found(args) if group_ids.empty? 112 121 … … 241 250 def users(exclude_user_id = nil) 242 251 return [] if self.user_groups.empty? 243 where_sql = "users. id = user_groups_users.user_id AND user_groups_users.user_group_id IN (#{self.user_groups.map {|g| g.id }.join(',')})"252 where_sql = "users.type = 'User' AND users.id = user_groups_users.user_id AND user_groups_users.user_group_id IN (#{self.user_groups.map {|g| g.id }.join(',')})" 244 253 where_sql = "users.id != #{exclude_user_id} AND " + where_sql if exclude_user_id.is_a?(Integer) 245 254 … … 252 261 # Returns true if the user is authorized to read this asset. 253 262 def authorized_to_read? 254 return false unless User.current_user 255 256 return !((User.current_user.user_groups & self.user_groups).empty?) 263 return false unless UserAccount.current_user 264 265 # Customer is authorized to read his assets. 266 return (self.customer == UserAccount.current_user) if UserAccount.current_user.class == Customer 267 268 return !((UserAccount.current_user.user_groups & self.user_groups).empty?) 257 269 end 258 270 … … 266 278 267 279 # Function: authorized_to_create_task? 268 # ==================================== ======280 # ==================================== 269 281 # 270 282 # Returns true if the user is authorized to create tasks to this asset 271 283 def authorized_to_create_task? 272 authorized_to(:create_task) 284 # Customer is authorized to create tasks to his assets if he has a service_request_type. 285 if UserAccount.current_user.class == Customer 286 return false unless UserAccount.current_user.service_request_type 287 return (self.customer == UserAccount.current_user) 288 end 289 290 return authorized_to(:create_task) 273 291 end 274 292 … … 451 469 # 452 470 def data_permission_profiles_for_current_user(datatype) 453 return [] unless user = User .current_user471 return [] unless user = UserAccount.current_user 454 472 455 473 group_ids = user.user_groups.collect { |g| g.id } … … 582 600 # :create_subasset, :edit_permissions, :remove. 583 601 def authorized_to(operation) 584 return false unless User .current_user602 return false unless UserAccount.current_user.class == User 585 603 586 604 authorized_user_groups = self.assets_user_groups.select { |aug| aug.send(operation) }.collect { |aug| aug.user_group } 587 return !(User .current_user.user_groups & authorized_user_groups).empty?605 return !(UserAccount.current_user.user_groups & authorized_user_groups).empty? 588 606 end 589 607 … … 833 851 return false 834 852 end 835 853 836 854 _info("Authorized to save #{self.class}(id=#{self.id})") 837 855 return true … … 1003 1021 # have to fulfill. 1004 1022 def Asset.count_authorized_to(operation, extra_conditions = nil) 1005 return false unless User .current_user1006 user_group_ids = User .current_user.user_groups.map {|g| g.id }1023 return false unless UserAccount.current_user 1024 user_group_ids = UserAccount.current_user.user_groups.map {|g| g.id } 1007 1025 return false if user_group_ids.empty? 1008 1026 case operation trunk/app/models/cmms_base.rb
r647 r707 11 11 12 12 include ModelObserver::Dispatcher 13 14 15 # Method: CmmsBase.validates_email_format 16 # ======================================= 17 # Validates format of an email address attribute. 18 # 19 # Parameters: 20 # ----------- 21 # attr_name - Name of the attribute to validate. 22 # options - An optional option Hash. 23 # 24 # Options: 25 # -------- 26 # allow_empty - If true an empty string is allowed. Default is false. 27 # 28 # See also: 29 # --------- 30 # Documentation of the method Base.validates_format_of for more information. 31 # 32 def CmmsBase.validates_email_format(attr_name, options=nil) 33 options ||= {} 34 options[:with] = /^[[:alnum:]!\#\$%&'\*\+-\/=\?^_`{|}~.]+@[[:alnum:]]+[.][[:alnum:]]+/ #' 35 if options.has_key?(:allow_empty) and options[:allow_empty] 36 options[:if] = Proc.new { |t| t.send(attr_name) != '' } 37 end 38 39 validates_format_of(attr_name, options) 40 end 13 41 end trunk/app/models/lockable.rb
r652 r707 20 20 21 21 22 # Returns +true+ if +User #current_user+ is22 # Returns +true+ if +UserAccount#current_user+ is 23 23 # allowed to modify the model, +false+ 24 24 # otherwise. … … 26 26 remove_timed_out_lock 27 27 28 return (not self.lock_user or User .current_user == self.lock_user)28 return (not self.lock_user or UserAccount.current_user == self.lock_user) 29 29 end 30 30 31 # Locks the model for +User #current_user+.31 # Locks the model for +UserAccount#current_user+. 32 32 # 33 33 # +duration+: How many seconds to lock for. … … 35 35 return false unless self.is_open_for_current_user 36 36 37 _info "Locking #{self.class} id=#{self.id} for user #{User.current_user.login_and_name} for duration: #{duration}."38 self.lock_user = User .current_user37 _info "Locking #{self.class} id=#{self.id} for #{UserAccount.current_user.class.to_s.downcase} #{UserAccount.current_user.login_and_name} for duration: #{duration}." 38 self.lock_user = UserAccount.current_user 39 39 self.lock_timeout = duration.from_now 40 40 return true trunk/app/models/message.rb
r577 r707 23 23 # Version of find that only finds message for the current user 24 24 def Message.find_for_current_user(*args) 25 return records_not_found(args) unless user = User .current_user25 return records_not_found(args) unless user = UserAccount.current_user 26 26 27 condition = "receiver_user_id = #{User .current_user.id.to_i}"27 condition = "receiver_user_id = #{UserAccount.current_user.id.to_i}" 28 28 29 29 options = extract_options_from_args!(args) trunk/app/models/protected_asset_data.rb
r576 r707 60 60 # Version of find that retuns objects which belong to assets the user is authorized read and whos attribute ready is +ready+. 61 61 def ProtectedAssetData.find_in_authorized_assets_with_ready(ready, args) 62 return records_not_found(args) unless user = User .current_user62 return records_not_found(args) unless user = UserAccount.current_user 63 63 64 64 group_ids = user.user_groups.map {|g| g.id } trunk/app/models/protected_data.rb
r655 r707 16 16 after_save :after_find 17 17 # Associations 18 belongs_to :creator, :class_name => 'User ', :foreign_key => 'created_by'18 belongs_to :creator, :class_name => 'UserAccount', :foreign_key => 'created_by' 19 19 20 20 # Like belongs_to association. In addition sets "asset model" for this data model … … 126 126 # See sub-classes for examples how this method can be used. 127 127 def ProtectedData.common_find_authorized_to(connection_model, asset_model_id_sql, operation, args) 128 return records_not_found(args) unless user = User .current_user128 return records_not_found(args) unless user = UserAccount.current_user 129 129 130 130 group_ids = user.user_groups.map {|g| g.id } … … 145 145 # See sub-classes for examples how this method can be used. 146 146 def ProtectedData.common_count_authorized_to(connection_model, asset_model_id_sql, operation, conditions = nil) 147 return 0 unless user = User .current_user147 return 0 unless user = UserAccount.current_user 148 148 149 149 group_ids = user.user_groups.map {|g| g.id } … … 268 268 after_find if new_record? # Sets @original_* 269 269 270 user = User .current_user270 user = UserAccount.current_user 271 271 272 272 if @original_ready … … 321 321 end 322 322 323 user = User .current_user323 user = UserAccount.current_user 324 324 self.creator = user 325 325 _info("New #{self.class} created by #{user.login}") … … 330 330 # according to user's permission profiles for the asset to which the data is created. 331 331 def create_data_permissions_from_users_permission_profiles 332 profiles = self.asset_model.data_permission_profiles_for_current_user(self.class) 333 _info("Creating permissions for a #{self.class} succeeded") if self.create_data_permissions_from_profiles(profiles) 332 case UserAccount.current_user.class.to_s 333 when 'User' 334 profiles = self.asset_model.data_permission_profiles_for_current_user(self.class) 335 _info("Creating permissions for a #{self.class} succeeded") if self.create_data_permissions_from_profiles(profiles) 336 when 'Customer' 337 # Everyone can read and edit data created by customers 338 self.other_read = true 339 self.other_change_state = true if has_change_state_permission? 340 self.other_edit = true 341 else 342 return false 343 end 344 334 345 return true 335 346 end trunk/app/models/protector_model.rb
r617 r707 57 57 # Checks that user is logged in 58 58 def check_user_logged_in 59 return true if User .current_user59 return true if UserAccount.current_user 60 60 61 61 _error("Have to be logged in to save #{self.class}") … … 65 65 # Checks that user is authorized to remove this object 66 66 def check_authorized_to_remove 67 unless User .current_user67 unless UserAccount.current_user 68 68 _error("Have to be logged in to remove #{self.class}") 69 69 return false trunk/app/models/task.rb
r652 r707 177 177 # Accept task and assing it to the current user 178 178 def accept 179 _info "Task #{self.id} accepted by #{User .current_user.login}."180 self.responsible_user = User .current_user179 _info "Task #{self.id} accepted by #{UserAccount.current_user.login}." 180 self.responsible_user = UserAccount.current_user 181 181 self.state = @@ACCEPTED 182 182 dispatch_event(:accepted) … … 185 185 # Close task 186 186 def close 187 _info "Task #{self.id} closed by #{User .current_user.login}."187 _info "Task #{self.id} closed by #{UserAccount.current_user.login}." 188 188 self.state = @@CLOSED 189 189 dispatch_event(:closed) … … 192 192 # Open task 193 193 def open 194 _info "Task #{self.id} opened by #{User .current_user.login}."194 _info "Task #{self.id} opened by #{UserAccount.current_user.login}." 195 195 self.state = @@NEW 196 196 self.responsible_user = nil trunk/app/models/task_type.rb
r652 r707 2 2 # See license agreement for additional rights 3 3 4 class TaskType < ActiveRecord::Base4 class TaskType < CmmsBase 5 5 # Protection 6 attr_protected :id 6 attr_protected :id, :is_service_request_type 7 7 # Validation 8 8 validates_presence_of :name … … 12 12 validates_associated :task_template 13 13 validates_length_of :notification_email, :maximum => 40 14 validates_ format_of :notification_email, :if => Proc.new { |t| t.notification_email != '' }, :with => /^[[:alnum:]!\#\$%&'\*\+-\/=\?^_`{|}~.]+@[[:alnum:]]+[.][[:alnum:]]+/14 validates_email_format :notification_email, :allow_empty => true 15 15 # Callbacks 16 16 before_validation :check_notification_email 17 after_save :update_is_service_request_type 17 18 # Associations 18 19 has_many :tasks 19 20 has_one :task_template, :dependent => :destroy 21 has_one :customer 20 22 21 23 exports_automatic_field 'cmms_task_type_name', :string, :name … … 25 27 def task_count 26 28 return self.tasks.count 29 end 30 31 # Method: TaskType.get_service_request_type 32 # ========================================= 33 # Get the service request type 34 # 35 def TaskType.get_service_request_type 36 return find_by_is_service_request_type(true) 37 end 38 39 # Method: set_as_service_request_type 40 # =================================== 41 # Set this task type to be the service request type 42 # 43 def set_as_service_request_type 44 return update_attribute(:is_service_request_type, true) 27 45 end 28 46 … … 35 53 self.notification_email = '' if self.notification_email == nil 36 54 end 55 56 # Method: update_is_service_request_type 57 # ====================================== 58 # Makes sure that only one record can have the is_service_request_type 59 # attribute true. When the attribute is set to true for a record, it 60 # will be set to false for all other records. 61 # 62 def update_is_service_request_type 63 TaskType.update_all(['is_service_request_type = ?', false], ['id != ? AND is_service_request_type = ?', self.id, true]) if self.is_service_request_type 64 return true 65 end 37 66 end trunk/app/models/timeline_event.rb
r103 r707 34 34 35 35 def before_save 36 self.creator = User .current_user unless self.creator36 self.creator = UserAccount.current_user unless self.creator 37 37 end 38 38 trunk/app/models/user.rb
r701 r707 2 2 # See license agreement for additional rights 3 3 4 require 'digest/sha1' 5 6 # this model expects a certain database layout and its based on the name/login pattern. 7 class User < ActiveRecord::Base 4 class User < UserAccount 8 5 # Protection 9 attr_protected : id6 attr_protected :full_name # this name is NOT used by this model 10 7 # Validation 11 validates_uniqueness_of :login 12 validates_confirmation_of :password 13 validates_length_of :login, :within => 3..40 14 validates_length_of :password, :within => 5..40 8 validates_presence_of :first_name, :last_name 15 9 validates_length_of :first_name, :maximum => 40 16 10 validates_length_of :last_name, :maximum => 40 17 validates_length_of :description, :maximum => 60 18 validates_length_of :email, :maximum => 40 19 validates_format_of :email, :if => Proc.new { |t| t.email != '' }, :with => /^[[:alnum:]!\#\$%&'\*\+-\/=\?^_`{|}~.]+@[[:alnum:]]+[.][[:alnum:]]+/ 20 validates_length_of :phone, :maximum => 20 21 validates_length_of :organization, :maximum => 40 22 validates_length_of :country, :maximum => 40 23 validates_length_of :city, :maximum => 40 24 validates_presence_of :login, :password, :first_name, :last_name 25 validates_presence_of :password_confirmation, :on => :create 11 validates_length_of :full_name, :maximum => 40, :allow_blank => true 26 12 # Callbacks 27 before_create :crypt_password28 before_validation_on_update :conserve_password_if_nil29 before_update :crypt_password_unless_empty30 after_save :unset_conserve_password31 after_find :unset_conserve_password32 13 # Associations 33 14 has_and_belongs_to_many :keyrings, :uniq => true, :order => 'name' … … 36 17 has_many :received_messages, :foreign_key => 'receiver_user_id', :order => 'created_at', :class_name => 'Message', :dependent => :delete_all 37 18 has_many :sent_messages, :foreign_key => 'sender_user_id', :order => 'created_at', :class_name => 'Message', :dependent => :delete_all 38 39 cattr_accessor :current_user40 41 exports_automatic_field 'cmms_creator_name', :string, :name42 exports_automatic_field 'cmms_creator_login', :string, :login43 exports_automatic_field 'cmms_creator_email', :string, :email44 exports_automatic_field 'cmms_creator_phone', :string, :phone45 46 19 47 20 # Function: User.model_name … … 59 32 end 60 33 61 # Users name 34 # Method: name 35 # ============ 36 # Get users name. This will overwrite the getter for the 'name' attribute 37 # which isn't used by this model. 62 38 def name 63 39 if self.last_name != "" … … 74 50 end 75 51 76 # Retuns "login (name)" 77 def login_and_name 78 unless self.name == "" 79 self.login + " (" + self.name + ")" 80 else 81 self.login 82 end 83 end 84 85 # Searches users by login 86 def User.search_by_login(login = '') 87 options = { :order => 'login' } 88 options[:conditions] = [sql_ilike('login', '?'), '%' + login + '%'] unless login.empty? 89 90 User.find(:all, options) 91 end 92 93 # Searches users by first and last name 52 # Method: search_by_name 53 # ====================== 54 # Searches users by first and last name. 55 # 56 # Returns: 57 # -------- 58 # An Array of users 59 # 94 60 def User.search_by_name(first_name = '', last_name = '') 95 61 options = { :order => 'last_name, first_name, login' } … … 109 75 end 110 76 111 # Updates accessed_at timestamp for +user+, without updating updated_at timestamp.112 # Returns the updated user.113 def User.accessing(user)114 timestamping = User.record_timestamps115 User.record_timestamps = false if timestamping116 User.update(user.id, {:accessed_at => Time.now, :password => nil})117 User.record_timestamps = true if timestamping118 User.find(user.id)119 end120 121 77 # Tasks assigned to the user 122 78 def tasks … … 124 80 end 125 81 126 # Please change the salt to something else, 127 # Every application should use a different one 128 @@salt = 'D0"TTL"Z(=5(JWT2MEA?' 129 130 # Authenticate a user. 131 # 132 # Example: 133 # @user = User.authenticate('bob', 'bobpass') 134 # 135 def self.authenticate(login, pass) 136 user = find(:first, :conditions => (["login = ? AND password = ?", login, sha1(pass)])) 137 return user 138 end 139 140 # Return true/false if User is authorized for resource. 141 def authorized?(resource) 142 # Multiple keys 143 if resource.class == Array 144 for key in resource 145 return true if action_key_strings.include?(key) 146 end 147 return false 148 end 149 150 # One key 151 return action_key_strings.include?(resource) 152 end 153 154 # Load action_key strings 82 # See the parent class for description 155 83 def action_key_strings 156 84 a = [] … … 158 86 a 159 87 end