Changeset 707

Show
Ignore:
Timestamp:
10/11/07 20:17:12 (1 year ago)
Author:
jarmo
Message:
  • Adds the Customer type
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app/controllers/application.rb

    r701 r707  
    203203    end 
    204204 
    205     User.current_user = session[:user] 
     205    UserAccount.current_user = session[:user] 
    206206    login = session[:user].login if session[:user] 
    207207    ActiveRecord::Base.logged_information(login, request.remote_ip) 
     
    299299  # redirect       - A dictionary specifying the url to which the client should 
    300300  #                  be redirected, if the object is created succesfully. 
     301  # partial_to_render - The partial view to render. 
    301302  # 
    302303  def common_create(model_class, name_attribute='name', 
     
    331332  # name_attribute - The name of the attribute of the model instance that returns 
    332333  #                  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  # 
    333341  def common_edit(model_class, name_attribute='name', 
    334342                  redirect={:action => 'index'}, 
    335343                  cancel_url_options={:action => 'index'}, 
    336                   partial_to_render='shared/create_or_edit') 
     344                  partial_to_render='shared/create_or_edit', 
     345                  partial_locals={}) 
    337346    # Find model unless it's already found 
    338347    @model ||= model_class.find(params[:id]) 
     
    348357    end 
    349358 
    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)) 
    351361  end 
    352362 
     
    396406 
    397407    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 
    398441  end 
    399442 
  • trunk/app/controllers/asset_controller.rb

    r702 r707  
    135135    end 
    136136 
     137    customer_choices = customer_choices_for_select 
     138 
    137139    @fields = [ 
    138140                [_('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 } ] 
    142142              ] 
     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 
    143147    @post_params = { :id => params[:id] } 
    144148    render(:partial => 'shared/create_or_edit', :layout => 'mainlevel', :locals => {:cancel_url_options => {:action => 'view' }}) 
     
    238242    end 
    239243 
     244    customer_choices = customer_choices_for_select 
     245 
    240246    @fields = [ 
    241247                [_('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 } ] 
    245249              ] 
     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']) 
    246253 
    247254    render(:partial => 'shared/create_or_edit', :layout => 'mainlevel', :locals => {:cancel_url_options => {:action => 'view'}}) 
     
    368375  end 
    369376 
     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 
    370382end 
    371383 
  • trunk/app/controllers/asset_permission_controller.rb

    r701 r707  
    271271    return if request.get? and action_name == 'list' 
    272272 
    273     return if @selected_asset.lock_user == User.current_user 
     273    return if @selected_asset.lock_user == UserAccount.current_user 
    274274 
    275275    unless @selected_asset.lock_for_current_user(LOCK_DURATION) 
  • trunk/app/controllers/task_type_controller.rb

    r705 r707  
    1818    @title = _('Task types') 
    1919    @fields = [ [_('Name'), :name], 
    20                 [_('Notification address'), :notification_email],  
     20                [_('Notification address'), :notification_email], 
    2121                [_('Description'), :description], 
    2222                [_('Template'), :task_template], 
     
    6363  def remove 
    6464    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? 
    6668        msg_could_not_remove_because_not_empty(instance) 
    6769      end 
  • trunk/app/controllers/timeline_controller.rb

    r488 r707  
    4747      next if ids.empty? 
    4848 
    49       id_conditions.push('(type = ? AND ' + klass.to_s.underscore + '_id IN (?))') 
     49      id_conditions.push('(timeline_events.type = ? AND timeline_events.' + klass.to_s.underscore + '_id IN (?))') 
    5050      event_conditions.push(klass.to_s + 'Event', ids) 
    5151    end 
  • trunk/app/controllers/user_controller.rb

    r701 r707  
    178178  end 
    179179 
     180  # Method: reset_password 
     181  # ====================== 
     182  # Resets password for a user 
     183  # 
    180184  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 
    197192  def login 
    198193    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!') 
    207239          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}" 
    208245        end 
    209246 
    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) 
    231249        session[:user_ip] = request.remote_ip 
    232250        session[:user_timeout] = 2.hours.from_now 
    233251        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." 
    237253 
    238254        expire_fragment(:controller => 'asset_tree', :action => 'asset_tree', :action_suffix => session[:user].login) 
    239         redirect_back_or_default :controller => 'asset', :action => "view" 
    240255      else 
    241256        error "Login failed for user #{params[:user_login]}." 
  • trunk/app/models/asset.rb

    r702 r707  
    77class Asset < ProtectorModel 
    88  # Protection 
    9   attr_accessible :code, :name, :description, :asset_type_id 
     9  attr_accessible :code, :name, :description, :asset_type_id, :customer_id 
    1010  # Validation 
    1111  validates_presence_of :name, :code 
     
    2424  # Associations 
    2525  belongs_to :asset_type 
     26  belongs_to :customer 
    2627  has_many :forms, :dependent => :destroy 
    2728  has_many :tasks, :dependent => :destroy 
     
    7374  def Asset.get_first_authorized_to_read 
    7475    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? 
    7677    return first 
    7778  end 
     
    9495  end 
    9596 
     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 
    96105  # Function: Asset.find_authorized_to 
    97106  # ================================== 
     
    106115  # 
    107116  def Asset.find_authorized_to(operation, args) 
    108     return records_not_found(args) unless User.current_user 
    109  
    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 } 
    111120    return records_not_found(args) if group_ids.empty? 
    112121 
     
    241250  def users(exclude_user_id = nil) 
    242251    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(',')})" 
    244253    where_sql = "users.id != #{exclude_user_id} AND " + where_sql if exclude_user_id.is_a?(Integer) 
    245254 
     
    252261  # Returns true if the user is authorized to read this asset. 
    253262  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?) 
    257269  end 
    258270 
     
    266278 
    267279  # Function: authorized_to_create_task? 
    268   # ========================================== 
     280  # ==================================== 
    269281  # 
    270282  # Returns true if the user is authorized to create tasks to this asset 
    271283  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) 
    273291  end 
    274292 
     
    451469  # 
    452470  def data_permission_profiles_for_current_user(datatype) 
    453     return [] unless user = User.current_user 
     471    return [] unless user = UserAccount.current_user 
    454472 
    455473    group_ids = user.user_groups.collect { |g| g.id } 
     
    582600  #             :create_subasset, :edit_permissions, :remove. 
    583601  def authorized_to(operation) 
    584     return false unless User.current_user 
     602    return false unless UserAccount.current_user.class == User 
    585603 
    586604    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? 
    588606  end 
    589607 
     
    833851      return false 
    834852    end 
    835        
     853 
    836854    _info("Authorized to save #{self.class}(id=#{self.id})") 
    837855    return true 
     
    10031021  #                    have to fulfill. 
    10041022  def Asset.count_authorized_to(operation, extra_conditions = nil) 
    1005     return false unless User.current_user 
    1006     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 } 
    10071025    return false if user_group_ids.empty? 
    10081026    case operation 
  • trunk/app/models/cmms_base.rb

    r647 r707  
    1111 
    1212  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 
    1341end 
  • trunk/app/models/lockable.rb

    r652 r707  
    2020 
    2121 
    22   # Returns +true+ if +User#current_user+ is 
     22  # Returns +true+ if +UserAccount#current_user+ is 
    2323  # allowed to modify the model, +false+ 
    2424  # otherwise. 
     
    2626    remove_timed_out_lock 
    2727 
    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) 
    2929  end 
    3030 
    31   # Locks the model for +User#current_user+. 
     31  # Locks the model for +UserAccount#current_user+. 
    3232  # 
    3333  # +duration+: How many seconds to lock for. 
     
    3535    return false unless self.is_open_for_current_user 
    3636 
    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_user 
     37    _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 
    3939    self.lock_timeout = duration.from_now 
    4040    return true 
  • trunk/app/models/message.rb

    r577 r707  
    2323  # Version of find that only finds message for the current user 
    2424  def Message.find_for_current_user(*args) 
    25     return records_not_found(args) unless user = User.current_user 
     25    return records_not_found(args) unless user = UserAccount.current_user 
    2626 
    27     condition = "receiver_user_id = #{User.current_user.id.to_i}" 
     27    condition = "receiver_user_id = #{UserAccount.current_user.id.to_i}" 
    2828 
    2929    options = extract_options_from_args!(args) 
  • trunk/app/models/protected_asset_data.rb

    r576 r707  
    6060  # Version of find that retuns objects which belong to assets the user is authorized read and whos attribute ready is +ready+. 
    6161  def ProtectedAssetData.find_in_authorized_assets_with_ready(ready, args) 
    62     return records_not_found(args) unless user = User.current_user 
     62    return records_not_found(args) unless user = UserAccount.current_user 
    6363 
    6464    group_ids = user.user_groups.map {|g| g.id } 
  • trunk/app/models/protected_data.rb

    r655 r707  
    1616  after_save :after_find 
    1717  # Associations 
    18   belongs_to :creator, :class_name => 'User', :foreign_key => 'created_by' 
     18  belongs_to :creator, :class_name => 'UserAccount', :foreign_key => 'created_by' 
    1919 
    2020  # Like belongs_to association. In addition sets "asset model" for this data model 
     
    126126  # See sub-classes for examples how this method can be used. 
    127127  def ProtectedData.common_find_authorized_to(connection_model, asset_model_id_sql, operation, args) 
    128     return records_not_found(args) unless user = User.current_user 
     128    return records_not_found(args) unless user = UserAccount.current_user 
    129129 
    130130    group_ids = user.user_groups.map {|g| g.id } 
     
    145145  # See sub-classes for examples how this method can be used. 
    146146  def ProtectedData.common_count_authorized_to(connection_model, asset_model_id_sql, operation, conditions = nil) 
    147     return 0 unless user = User.current_user 
     147    return 0 unless user = UserAccount.current_user 
    148148 
    149149    group_ids = user.user_groups.map {|g| g.id } 
     
    268268    after_find if new_record? # Sets @original_* 
    269269 
    270     user = User.current_user 
     270    user = UserAccount.current_user 
    271271 
    272272    if @original_ready 
     
    321321    end 
    322322 
    323     user = User.current_user 
     323    user = UserAccount.current_user 
    324324    self.creator = user 
    325325    _info("New #{self.class} created by #{user.login}") 
     
    330330  # according to user's permission profiles for the asset to which the data is created. 
    331331  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 
    334345    return true 
    335346  end 
  • trunk/app/models/protector_model.rb

    r617 r707  
    5757  # Checks that user is logged in 
    5858  def check_user_logged_in 
    59     return true if User.current_user 
     59    return true if UserAccount.current_user 
    6060 
    6161    _error("Have to be logged in to save #{self.class}") 
     
    6565  # Checks that user is authorized to remove this object 
    6666  def check_authorized_to_remove 
    67     unless User.current_user 
     67    unless UserAccount.current_user 
    6868      _error("Have to be logged in to remove #{self.class}") 
    6969      return false 
  • trunk/app/models/task.rb

    r652 r707  
    177177  # Accept task and assing it to the current user 
    178178  def accept 
    179     _info "Task #{self.id} accepted by #{User.current_user.login}." 
    180     self.responsible_user = User.current_user 
     179    _info "Task #{self.id} accepted by #{UserAccount.current_user.login}." 
     180    self.responsible_user = UserAccount.current_user 
    181181    self.state = @@ACCEPTED 
    182182    dispatch_event(:accepted) 
     
    185185  # Close task 
    186186  def close 
    187     _info "Task #{self.id} closed by #{User.current_user.login}." 
     187    _info "Task #{self.id} closed by #{UserAccount.current_user.login}." 
    188188    self.state = @@CLOSED 
    189189    dispatch_event(:closed) 
     
    192192  # Open task 
    193193  def open 
    194     _info "Task #{self.id} opened by #{User.current_user.login}." 
     194    _info "Task #{self.id} opened by #{UserAccount.current_user.login}." 
    195195    self.state = @@NEW 
    196196    self.responsible_user = nil 
  • trunk/app/models/task_type.rb

    r652 r707  
    22# See license agreement for additional rights 
    33 
    4 class TaskType < ActiveRecord::Base 
     4class TaskType < CmmsBase 
    55  # Protection 
    6   attr_protected :id 
     6  attr_protected :id, :is_service_request_type 
    77  # Validation 
    88  validates_presence_of :name 
     
    1212  validates_associated :task_template 
    1313  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 
    1515  # Callbacks 
    1616  before_validation :check_notification_email 
     17  after_save :update_is_service_request_type 
    1718  # Associations 
    1819  has_many :tasks 
    1920  has_one :task_template, :dependent => :destroy 
     21  has_one :customer 
    2022 
    2123  exports_automatic_field 'cmms_task_type_name', :string, :name 
     
    2527  def task_count 
    2628    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) 
    2745  end 
    2846 
     
    3553    self.notification_email = '' if self.notification_email == nil 
    3654  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 
    3766end 
  • trunk/app/models/timeline_event.rb

    r103 r707  
    3434 
    3535  def before_save 
    36     self.creator = User.current_user unless self.creator 
     36    self.creator = UserAccount.current_user unless self.creator 
    3737  end 
    3838 
  • trunk/app/models/user.rb

    r701 r707  
    22# See license agreement for additional rights 
    33 
    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 
     4class User < UserAccount 
    85  # Protection 
    9   attr_protected :id 
     6  attr_protected :full_name  # this name is NOT used by this model 
    107  # 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 
    159  validates_length_of :first_name, :maximum => 40 
    1610  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 
    2612  # Callbacks 
    27   before_create :crypt_password 
    28   before_validation_on_update :conserve_password_if_nil 
    29   before_update :crypt_password_unless_empty 
    30   after_save :unset_conserve_password 
    31   after_find :unset_conserve_password 
    3213  # Associations 
    3314  has_and_belongs_to_many :keyrings, :uniq => true, :order => 'name' 
     
    3617  has_many :received_messages, :foreign_key => 'receiver_user_id', :order => 'created_at', :class_name => 'Message', :dependent => :delete_all 
    3718  has_many :sent_messages, :foreign_key => 'sender_user_id', :order => 'created_at', :class_name => 'Message', :dependent => :delete_all 
    38  
    39   cattr_accessor :current_user 
    40  
    41   exports_automatic_field 'cmms_creator_name', :string, :name 
    42   exports_automatic_field 'cmms_creator_login', :string, :login 
    43   exports_automatic_field 'cmms_creator_email', :string, :email 
    44   exports_automatic_field 'cmms_creator_phone', :string, :phone 
    45  
    4619 
    4720  # Function: User.model_name 
     
    5932  end 
    6033 
    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. 
    6238  def name 
    6339    if self.last_name != "" 
     
    7450  end 
    7551 
    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  # 
    9460  def User.search_by_name(first_name = '', last_name = '') 
    9561    options = { :order => 'last_name, first_name, login' } 
     
    10975  end 
    11076 
    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_timestamps 
    115     User.record_timestamps = false if timestamping 
    116     User.update(user.id, {:accessed_at => Time.now, :password => nil}) 
    117     User.record_timestamps = true if timestamping 
    118     User.find(user.id) 
    119   end 
    120  
    12177  # Tasks assigned to the user 
    12278  def tasks 
     
    12480  end 
    12581 
    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 
    15583  def action_key_strings 
    15684    a = [] 
     
    15886    a 
    15987  end