Changeset 357

Show
Ignore:
Timestamp:
12/18/06 16:54:39 (2 years ago)
Author:
jarmo
Message:

* Merged changesets [327], [328], [331], [332], [333], [334], [335], [336], [338], [342], [343], [344], [345], [346], [347], [348], [349], [351], [354], [356] and parts of changesets [329], [330], [350], [352], [355] to the 1.0 branch

Files:

Legend:

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

    r326 r357  
    99 
    1010# Class: ApplicationController 
    11 # ================================= 
     11# ============================ 
    1212# 
    1313# The filters added to this controller will be run for all controllers in the application. 
     
    206206        flash[:notice] = msg_create_successful(@model) 
    207207        redirect_to(redirect) and return 
    208       else 
     208      elsif @model.errors.empty? 
    209209        flash[:error] = msg_system_failure_in(_('Creation')) 
    210210      end 
     
    233233        flash[:notice] = msg_changes_saved 
    234234        redirect_to(redirect) and return 
    235       else 
     235      elsif @model.errors.empty? 
    236236        flash[:notice] = msg_saving_failed 
    237237      end 
     
    284284    } 
    285285 
    286     flash[:notice] = _("Removed %d %s", num_instances_deleted, _(Localization.human_readable_model_name(model_class).pluralize)) if num_instances_deleted > 1 
     286    flash[:notice] = _("Removed %d %s", num_instances_deleted, _(Localization.human_readable_model_name(model_class).pluralize)) if num_instances_deleted > 0 
    287287  end 
    288288 
     
    325325  end 
    326326 
     327  # Function: find_selected_assets 
     328  # ============================== 
     329  # 
     330  # Finds selected assets and constructs conditions for further database queries 
     331  # from selected assets. 
    327332  def find_selected_assets 
    328333    if session[:select_branches] 
    329334      if session[:branch_depth] and session[:branch_depth] > 0 
    330         @selected_assets = Asset.find_authorized_to_read(:all, :conditions => ['assets.id IN (?)', @selected_asset.id_and_subasset_ids(session[:branch_depth])]) 
     335        conditions = ['assets.id IN (?)', @selected_asset.id_and_subasset_ids(session[:branch_depth])] 
    331336      else 
    332         @selected_assets = Asset.find_authorized_to_read(:all, :conditions => ['assets.id IN (?)', @selected_asset.id_and_subasset_ids]) 
    333       end 
    334  
     337        conditions = ['assets.id IN (?)', @selected_asset.id_and_subasset_ids] 
     338      end 
    335339      if @selected_asset_type 
    336         @selected_assets.delete_if { |asset| asset.asset_type_id != @selected_asset_type.id } 
    337       end 
     340        conditions[0] += ' AND asset_type_id = ?' 
     341        conditions.push @selected_asset_type.id 
     342      end 
     343      @selected_assets = Asset.find_authorized_to_read(:all, :conditions => conditions) 
    338344    else 
    339345      @selected_assets = [@selected_asset] 
     
    364370    if session[:view_as_list] 
    365371      @assets_by_type = {} 
    366       if session[:select_branches] 
    367         ids = @selected_asset.id_and_subasset_ids - [@selected_asset.id] 
    368         assets = Asset.find_authorized_to_read(ids) 
    369       else 
    370         assets = @selected_asset.children 
    371       end 
    372372      find_selected_assets 
    373       assets = @selected_assets 
    374       assets.each { |child| 
    375         if child.authorized_to_read? 
    376           if child.asset_type 
    377             (@assets_by_type[child.asset_type.id] ||= []).push(child) 
    378           else 
    379             (@assets_by_type[:typeless] ||= []).push(child) 
    380           end 
     373      @selected_assets.each { |asset| 
     374        if asset.asset_type 
     375          (@assets_by_type[asset.asset_type_id] ||= []).push(asset) 
     376        else 
     377          (@assets_by_type[:typeless] ||= []).push(asset) 
    381378        end 
    382379      } 
    383380    else 
    384381      @open_node_list = session[:open_tree_node_list] 
    385       @nodes = Asset.find(:all, :readonly => 'true', :conditions => 'parent_id is null') 
    386382    end 
    387383  end 
  • branches/1.0/app/controllers/asset_controller.rb

    r318 r357  
    6060    @new_asset.parent = @selected_asset 
    6161    @new_asset.use_parents_permissions = true 
    62  
    63     flash[:error] = msg_saving_failed and return unless @new_asset.save 
    64  
    65     flash[:notice] = _('Asset %s created as a subasset of %s.', @new_asset.code_and_name, @selected_asset.code_and_name) 
    66     redirect_to :action => 'view' 
    67     exipre_asset_tree_fragments if request.post? 
     62     
     63    if @new_asset.save 
     64      flash[:notice] = _('Asset %s created as a subasset of %s.', @new_asset.code_and_name, @selected_asset.code_and_name) 
     65      redirect_to :action => 'view' 
     66      exipre_asset_tree_fragments 
     67    elsif @new_asset.errors.empty? 
     68      flash[:error] = msg_saving_failed 
     69    end 
    6870  end 
    6971 
     
    105107 
    106108      unless @selected_asset.update_attributes(params[:selected_asset]) 
    107         flash[:error] = msg_saving_failed 
     109        flash[:error] = msg_saving_failed if @selected_asset.errors.empty? 
    108110        return 
    109111      end 
  • branches/1.0/app/controllers/asset_permission_controller.rb

    r305 r357  
    3333 
    3434    @title = _('User groups that have permissions to asset %s', @selected_asset.code_and_name) 
    35     @fields = [  
     35    @fields = [ 
    3636                [_('Name'), :name], 
    37                 [_('Information'), :information]  
     37                [_('Information'), :information] 
    3838              ] 
    3939    @items = @selected_asset.user_groups 
     
    9393    remove_count = asset.remove_user_groups(params['delete'].keys) 
    9494 
     95    unless remove_count 
     96      flash[:notice] = _('Forbidden action: You tried to remove all user groups, which have permission to edit asset permissions.') 
     97      return 
     98    end 
     99 
    95100    if remove_count > 0 
    96101      flash[:notice] = _('Removed this asset from %d user groups.', remove_count) 
     
    108113 
    109114    if @selected_asset.update_attribute(:use_parents_permissions, params[:selected_asset][:use_parents_permissions]) 
    110       flash[:notice] = msg_changes_saved  
    111     else 
     115      flash[:notice] = msg_changes_saved 
     116    elsif @selected_asset.errors.empty? 
    112117      flash[:error] = msg_saving_failed 
    113118    end 
     
    175180 
    176181    # Update user group's permissions to the asset. 
    177     if assets_user_group.update_permissions(params[:assets_user_group]) 
    178  
    179       info("AssetsUserGroup(id=#{assets_user_group.id}) updated.") 
    180       redirect_with_message(msg_changes_saved, 
    181                             :action => 'edit', 
    182                             :id => assets_user_group.user_group_id, 
    183                             :asset_id => assets_user_group.asset_id) 
    184     else 
    185       error("Saving AssetsUserGroup(id=#{assets_user_group.id}) FAILED!") 
    186       flash[:error] = msg_saving_failed 
    187     end 
     182    case assets_user_group.update_permissions(params[:assets_user_group]) 
     183      when true 
     184        info("AssetsUserGroup(id=#{assets_user_group.id}) updated.") 
     185        flash[:notice] = msg_changes_saved 
     186      when nil 
     187        flash[:notice] = _('Forbidden action: You tried to remove the "Edit asset permissions" permission and this user group is the only one with that permission.') 
     188      else # false 
     189        error("Saving AssetsUserGroup(id=#{assets_user_group.id}) FAILED!") 
     190        flash[:error] = msg_saving_failed 
     191    end 
     192    redirect_to(:action => 'edit', 
     193                :id => assets_user_group.user_group_id, 
     194                :asset_id => assets_user_group.asset_id) 
    188195  end 
    189196 
     
    194201  def update_permission_profiles 
    195202    invalid_request and return unless request.post? 
    196      
     203 
    197204    # find object and check that authorized to edit and that asset it belongs to isn't using parent's permissions. 
    198205    assets_user_group = AssetsUserGroup.find(params[:id]) 
  • branches/1.0/app/controllers/data_permission_controller.rb

    r305 r357  
    9292      if @object.update_permissions(params[:permission], params[:object]) 
    9393        flash[:notice] = msg_changes_saved 
    94       else 
     94      elsif @object.errors.empty? 
    9595        flash[:error] = msg_saving_failed 
    9696      end 
  • branches/1.0/app/controllers/message_controller.rb

    r305 r357  
    3838    if @message.unread 
    3939      @message.unread = false 
    40       unless @message.save 
     40      unless @message.save and @message.errors.empty? 
    4141        flash[:error] = msg_saving_failed 
    4242      end 
  • branches/1.0/app/controllers/report_controller.rb

    r326 r357  
    150150        @report.editable = params[:report][:editable] if params[:report] and @report.report_type.editable 
    151151        session[:image_fields].delete(@report_cookie) if session[:image_fields] 
    152         if @report.save 
    153           # FIXME: This could be cleaner 
    154           for value in @report.report_field_values 
    155             value.save 
    156           end 
     152        if @report.save_with_values 
    157153          flash[:notice] = msg_changes_saved 
    158154          redirect_to :action => 'view', :id => @report.id and return 
  • branches/1.0/app/controllers/report_type_controller.rb

    r305 r357  
    165165    if request.post? 
    166166      @new_field_group = ReportFieldGroup.new params['new_field_group'] 
    167       @new_field_group.report_type 
     167      @new_field_group.report_type = @report_type 
     168      @new_field_group.place = @new_field_group.report_type.report_field_groups.size + 1 
    168169      if @new_field_group.valid? 
    169170        @report_type.report_field_groups << @new_field_group 
     
    235236    @groups = @report_type.report_field_groups 
    236237    @available_field_groups = [ [_('Not in a group'), nil] ] + @groups.collect {|g| [g.name, g.id]} 
    237     @place_options = Array.new(@fields.size) { |i| i += 1; [ "#{i}.", i ] } 
     238    # Make sure that all current places are found in the options 
     239    @group_place_options = (@groups.collect { |g| g.place } + (1..@groups.size).to_a).sort.uniq.collect { |i|  ["#{i}.", i] } 
    238240    @new_field_group = ReportFieldGroup.new 
    239241  end 
  • branches/1.0/app/controllers/user_controller.rb

    r326 r357  
    7575                       [_('Description'), :description] 
    7676                      ] 
    77     @actions = [ 
    78                 [_('Permissions'), {:controller => 'keyring', :action => 'edit_keys', :image => '16x16/permissions'} ], 
    79                 [_('Users'), {:controller => 'keyring', :action => 'edit_owners', :image => '16x16/user_group'} ] 
     77    @keyring_actions = [ 
     78                [_('Keys'), {:controller => 'keyring', :action => 'edit_keys', :image => '16x16/keys'} ], 
     79                [_('Owners'), {:controller => 'keyring', :action => 'edit_owners', :image => '16x16/user_group'} ] 
    8080               ] 
    8181    @keyring_post_params = { :action => 'remove_from_keyrings', :id => @model.id } 
    82     @items = @model.keyrings 
     82    @keyring_items = @model.keyrings 
     83     
     84    # User group list 
     85    @user_group_fields = [ 
     86                       [_('Name'), :name], 
     87                       [_('Description'), :description] 
     88                      ] 
     89    @user_group_actions = [ [_('Users'), {:controller => 'user_group', :action => 'users', :image => '16x16/user_group' }] ] 
     90    @user_group_post_params = { :action => 'remove_from_user_groups', :id => @model.id } 
     91    @user_group_items = @model.user_groups 
    8392 
    8493    if request.post? 
     
    8897        flash[:notice] = _('User information changed') 
    8998        info "User information changed for #{@model.login}" 
    90       else 
     99      elsif @model.errors.empty? 
    91100        flash[:error] = _('Unable to change user information') 
    92101      end 
     
    98107 
    99108    # Ignore GET requests 
    100     return if request.get? 
     109    return if request.get? or not params[:delete] or params[:delete].empty? 
    101110 
    102111    names = [] 
     
    107116    end 
    108117    flash[:notice] = _('User %s removed from keyrings: %s', @user.login_and_name, names.join(',')) 
     118  end 
     119 
     120  def remove_from_user_groups 
     121    redirect_to :back 
     122 
     123    # Ignore GET requests 
     124    return if request.get? or not params[:delete] or params[:delete].empty? 
     125 
     126    names = [] 
     127    @user = User.find(params[:id]) 
     128    for user_group in @user.user_groups.find(params[:delete].keys) 
     129      user_group.users.delete(@user) 
     130      names.push user_group.name 
     131    end 
     132    flash[:notice] = _('User %s removed from user groups: %s', @user.login_and_name, names.join(',')) 
    109133  end 
    110134 
  • branches/1.0/app/controllers/work_order_controller.rb

    r318 r357  
    105105    @users = @work_order.asset.users(@work_order.responsible_user_id) 
    106106     
    107     @title = _('Work order %s (%s)', @work_order.to_s, @work_order.work_order_type.name) 
     107    @wo_title = _('Work order %s (%s)', @work_order.to_s, @work_order.work_order_type.name) 
    108108 
    109109    if request.post? 
  • branches/1.0/app/helpers/application_helper.rb

    r318 r357  
    103103    last_year = options[:first_year] + options[:years] 
    104104    options[:include_time] = false unless options.has_key?(:include_time) 
    105     id_prefix = prefix.gsub('][', '_').gsub(/\[|\]/, '_') 
     105    id_prefix = prefix.gsub('][', '_').gsub('[', '_').gsub(/\]$/, '') 
    106106 
    107107    select = select_tag("#{prefix}[year]", year_options(time.year, options[:first_year], last_year), {'id' => "#{id_prefix}_year"}) + 
     
    543543  # Function: link_class_for_asset_tree_node 
    544544  # ======================================== 
    545   def link_class_for_asset_tree_node(node, depth) 
    546     is_selected_type = (@selected_asset_type == nil or (@selected_asset_type and node.asset_type_id == @selected_asset_type.id)) 
    547  
    548     if @selected_asset == node 
     545  def link_class_for_asset_tree_node(asset_id, asset_type_id, depth) 
     546    is_selected_type = (@selected_asset_type == nil or (@selected_asset_type and asset_type_id == @selected_asset_type.id)) 
     547 
     548    if @selected_asset.id == asset_id 
    549549      if is_selected_type or not session[:select_branches] 
    550550        return 'asset_tree_selected_asset' 
     
    566566  # ======================================== 
    567567  def span_class_for_asset_tree_node(node) 
    568     return node.asset_type ? "asset_type_#{node.asset_type.id}" : "asset_type_0" 
     568    return node.asset_type ? "asset_type_#{node.asset_type_id}" : "asset_type_0" 
    569569  end 
    570570 
    571571  # Function: cancel_tag 
    572   # ======================= 
     572  # ==================== 
    573573  # 
    574574  # Retuns a cancellation submit button for forms. 
     
    640640    table + end_form_tag 
    641641  end 
     642 
     643  @@asset_code_and_name_sql = ActiveRecord::Base.connection.sql_concat(:code, ' (', :name, ')') 
     644  def find_asset_tree_nodes(group_ids, parent_id = nil, open_parent = true, branch_depth = nil) 
     645    parent_condition = parent_id ? "parent_id = #{parent_id}" : 'parent_id IS NULL' 
     646    create_subasset_aug_count = session[:move_mode] ? ", (SELECT COUNT(*) FROM assets_user_groups WHERE (asset_id = assets.id AND create_subasset = 1 AND user_group_id IN (#{group_ids.join(',')}))) AS create_subasset_aug_count" : '' 
     647    nodes = Asset.connection.execute("SELECT id, #@@asset_code_and_name_sql, asset_type_id, (SELECT COUNT(*) FROM assets T1 WHERE T1.parent_id = assets.id) AS child_count, (SELECT COUNT(*) FROM assets_user_groups WHERE (asset_id = assets.id AND user_group_id IN (#{group_ids.join(',')}))) AS aug_count #{create_subasset_aug_count} FROM assets WHERE #{parent_condition}") 
     648 
     649    results = [] 
     650    for node in nodes 
     651      result = "" 
     652      is_open = (open_parent and @open_node_list and @open_node_list.include? node[0].to_i) 
     653      depth = @selected_asset.id == node[0].to_i ? 0 : branch_depth 
     654      is_authorized = node[4] != '0' 
     655 
     656      # The node above this one is closed so we only need to look for 
     657      # authorized children 
     658      unless open_parent 
     659        return 's' if is_authorized # Found authorized children (return values is not used but it should be other than "") 
     660      end 
     661 
     662      # Recurse if node has children 
     663      children = node[3] == '0' ? '' : find_asset_tree_nodes(group_ids, node[0], is_open, depth ? depth + 1 : nil)  
     664 
     665      # Don't display this node if the user is unauthorized to read it and 
     666      # there were no authorized sub-assets 
     667      next unless is_authorized or children == "" 
     668 
     669      is_leaf = (children == "") 
     670      link_text = '<span class="' + (node[2] ? 'asset_type_' + node[2] : 'asset_type_0') + '">' + node[1] + '</span>' 
     671      tree_symbol = is_open ? image_tag('16x16/folder_min.gif') : image_tag('16x16/folder_plu.gif') 
     672 
     673      result += '<li class="' + (is_leaf ? 'tree_leaf' : 'tree') + '">' 
     674      result += '<a href="/asset_tree/toggle_tree_node_open?node_id=' + node[0] + '" class="tree_open_close">' + tree_symbol + '</a>' unless is_leaf 
     675      result += radio_button_tag('new_parent_id', node[0], false, :disabled => (@selected_asset.id == node[0].to_i or (branch_depth and branch_depth > 0) or node[5] == '0')) if session[:move_mode] 
     676      result += is_authorized ? '<a href="/asset_tree/select/' + node[0] + '" class="' + link_class_for_asset_tree_node(node[0].to_i, node[2].to_i, branch_depth) + '">' + link_text + '</a>' : link_text 
     677      result += '<ul class="tree">' + children + '</ul>' unless is_leaf or not is_open 
     678      result += '</li>' 
     679      results.push result 
     680    end 
     681    return results.join 
     682  end 
    642683end 
    643  
  • branches/1.0/app/models/asset.rb

    r105 r357  
    22# See license agreement for additional rights 
    33 
     4# Class: Asset 
     5# ============ 
     6# 
    47class Asset < ProtectorModel 
    58  # Protection 
     
    2629  acts_as_tree :order => 'code' 
    2730 
     31  # Function: Asset.minimal_select 
     32  # ============================== 
     33  # 
    2834  # Returns the smallest possible SELECT clause that can be used in a find call for this model. 
    2935  # Contains protected fields and +use_parents_permissions+. 
     
    3238  end 
    3339 
     40  # Function: Asset.full_code_for 
     41  # ============================= 
     42  # 
    3443  # Returns the full code for asset with the given id or nil 
     44  # 
     45  # Parameters: 
     46  # ----------- 
     47  # id - Asset id 
     48  # 
    3549  def Asset.full_code_for(id) 
    3650    begin 
     
    4155  end 
    4256 
     57  # Function: Asset.get_first_authorized_to_read 
     58  # ============================================ 
     59  # 
    4360  # Returns the first found asset user is authorized to read. 
    4461  # If user is unauthorized to read any assets raises an exception. 
     
    4966  end 
    5067 
     68  # Function: Asset.find_by_full_code 
     69  # ================================= 
     70  # 
    5171  # Finds assets user is authorized to read by the full code. 
     72  # 
     73  # Parameters: 
     74  # ----------- 
     75  # full_code - Full code of an asset 
     76  # 
    5277  def Asset.find_by_full_code(full_code) 
    5378    code = full_code.split('-')[-1] 
     
    5883  end 
    5984 
    60   # Version of find which only returns assets the user is authorized to perform +operation+. 
    61   # +operation+::  Symbol defining which permission is checked. One of :read, :edit, :create_subasset, 
    62   #                :remove, :edit_permissions, :create_report, :create_work_order, :attach_file. 
     85  # Function: Asset.find_authorized_to 
     86  # ================================== 
     87  # 
     88  # Version of find which only returns assets the user is authorized to perform the given operation. 
     89  # 
     90  # Parameters: 
     91  # ----------- 
     92  # operation - Symbol defining which permission is checked. Possible operations 
     93  #             are :edit, :create_report, :create_work_order, :attach_file, 
     94  #             :create_subasset, :edit_permissions, :remove. 
     95  # 
    6396  def Asset.find_authorized_to(operation, args) 
    6497    return records_not_found(args) unless User.current_user 
     
    84117  end 
    85118 
     119  # Function: full_code 
     120  # =================== 
     121  # 
    86122  # Returns the asset's full code. 
    87123  # 
    88   # The full code is the asset's code prefixed with 
    89   # +Asset#code_prefix()+. 
     124  # The full code is the asset's code prefixed with Asset#code_prefix(). 
     125  # 
    90126  def full_code 
    91127    return self.code_prefix + self.code 
    92128  end 
    93129 
     130  # Function: code_prefix 
     131  # ===================== 
     132  # 
    94133  # Returns the asset's code prefix. 
    95134  # 
     
    104143  end 
    105144 
    106   # Returns: 'code (name)' 
     145  # Function: code_and_name 
     146  # ======================= 
     147  # 
     148  # Returns code and name in from 'code (name)' 
    107149  def code_and_name 
    108150    self.code + ' (' + self.name + ')' 
    109151  end 
    110152 
     153  # Function: is_root? 
     154  # 
    111155  # Returns true if asset is the root asset. 
    112156  def is_root? 
     
    114158  end 
    115159 
    116   # Returns asset's id and ids of the sub-assets +levels+ levels down. 
     160  # Function: id_and_subasset_ids 
     161  # ============================= 
     162  # 
     163  # Returns asset's id and ids of the sub-assets given number of levels down. 
    117164  # i.e. returns asset's id and it's children's ids and their chilren's ids etc. 
    118165  def id_and_subasset_ids(levels = nil) 
     
    124171  end 
    125172 
     173  # Function: path_to_root 
     174  # ====================== 
     175  # 
    126176  # Returns assets that are in the path from this asset to the root asset. 
    127177  def path_to_root 
     
    130180  end 
    131181 
     182  # Function: users 
     183  # =============== 
     184  # 
    132185  # Returns all users who belong to assets user groups. 
    133   # If +exclude_user_id+ is given, user with given id is excluded from returned users. 
     186  # 
     187  # Parameters: 
     188  # ----------- 
     189  # exclude_user_id - user with this id is excluded from returned users. Is optional. 
     190  # 
    134191  def users(exclude_user_id = nil) 
    135192    return [] if self.user_groups.empty? 
     
    140197  end 
    141198 
     199  # Function: authorized_to_read? 
     200  # ============================= 
     201  # 
    142202  # Returns true if the user is authorized to read this asset. 
    143203  def authorized_to_read? 
     
    147207  end 
    148208 
     209  # Function: authorized_to_create_report? 
     210  # ====================================== 
     211  # 
    149212  # Returns true if the user is authorized to create reports to this asset 
    150213  def authorized_to_create_report? 
     
    152215  end 
    153216 
     217  # Function: authorized_to_create_work_order? 
     218  # ========================================== 
     219  # 
    154220  # Returns true if the user is authorized to create work_orders to this asset 
    155221  def authorized_to_create_work_order? 
     
    157223  end 
    158224 
     225  # Function: authorized_to_attach_file? 
     226  # ==================================== 
     227  # 
    159228  # Returns true if the user is authorized to create attachments to this asset 
    160229  def authorized_to_attach_file? 
     
    162231  end 
    163232 
     233  # Function: authorized_to_create_subasset? 
     234  # ======================================== 
     235  # 
    164236  # Returns true if the user is authorized to create sub-assets to this asset 
    165237  def authorized_to_create_subasset? 
     
    167239  end 
    168240 
     241  # Function: authorized_to_edit_permissions? 
     242  # ========================================= 
     243  # 
    169244  # Returns true if the user is authorized to edit asset permissions for this asset 
    170245  def authorized_to_edit_permissions? 
     
    172247  end 
    173248 
     249  # Function: authorized_to_remove? 
     250  # =============================== 
     251  # 
    174252  # Returns true if the user is authorized to remove this asset 
    175253  def authorized_to_remove? 
     
    177255  end 
    178256 
     257  # Function: authorized_to_create? 
     258  # =============================== 
     259  # 
    179260  # Returns true if the user is authorized to create new instances of +datatype+ to this asset 
    180261  def authorized_to_create?(datatype) 
     
    191272  end  
    192273 
     274  # Function: has_subasset_authorized_to_read? 
     275  # ========================================== 
     276  # 
    193277  # Returns true if asset has a sub-asset user is authorized to read. 
    194278  def has_subasset_authorized_to_read? 
     
    200284  end 
    201285 
    202   # Creates new Asset <-> UserGroup associattion between this asset and user group 
    203   # +user_group+ i.e. gives user group +user_group+ permission to read this asset. 
    204   # If the second (optional) parameter +edit+ is true, also editing permission will be given. 
     286  # Function: add_user_group 
     287  # ======================== 
     288  # 
     289  # Creates new Asset <-> UserGroup associattion between this asset and the given 
     290  # user group i.e. gives the user group permission to read this asset. 
    205291  # The same association is created also for all sub-assets using this asset's permissions. 
    206292  # Returns true if the creation is successfull and returns false if it fails. 
     293  # 
     294  # Parameters: 
     295  # ----------- 
     296  # user_group  - User group to add to this asset. 
     297  # permissions - Optional hash containing additional permissions to the user group, 
     298  #               e.g. :edit => true. Default {}. 
     299  # 
    207300  def add_user_group(user_group, permissions = {}) 
    208301    raise("#{user_group.class} in not a UserGroup") unless user_group.is_a?(UserGroup) 
     
    222315  end 
    223316 
     317  # Function: remove_user_groups 
     318  # ============================ 
     319  # 
    224320  # Removes user groups with ids +user_group_ids+ from this asset i.e. removes 
    225321  # all permissions to this asset from user groups with ids +user_group_ids+. 
    226   # Returns true if the removal is successfull and returns false if it fails. 
     322  # Returns number of removed user groups if the removal is successfull and 
     323  # returns nil if it fails. 
    227324  def remove_user_groups(user_group_ids) 
    228325    raise('Unable to remove user groups without ids') if user_group_ids.empty? 
     
    231328    associations_to_destroy = self.assets_user_groups.find(:all, :conditions => ['user_group_id IN (?)', user_group_ids]) 
    232329    remove_count = 0 
    233     associations_to_destroy.each { |association| 
    234  
    235       if association.destroy 
    236         remove_count += 1 
    237         _info("User group (id=#{association.user_group_id}) removed from asset #{self.full_code} and from all sub-assets using it's permissions") 
    238       else 
    239         _error("Unable to remove user group (id=#{association.user_group_id}) from asset #{self.full_code}") 
     330    begin 
     331      AssetsUserGroup.transaction do 
     332        associations_to_destroy.each { |association| 
     333          if association.destroy 
     334            remove_count += 1 
     335            _info("User group (id=#{association.user_group_id}) removed from asset #{self.full_code} and from all sub-assets using it's permissions") 
     336          else 
     337            _error("Unable to remove user group (id=#{association.user_group_id}) from asset #{self.full_code}") 
     338          end 
     339        } 
     340        raise(InvalidAssetPermissions) unless self.permissions_valid? 
    240341      end 
    241     } 
     342    rescue InvalidAssetPermissions 
     343      _info("Tried to remove all user groups with the \"Edit permissions\" permission in asset #{self.full_code}") 
     344      return nil 
     345    end 
    242346    return remove_count 
    243347  end 
    244348 
    245   # Returns current user's data permission profiles for data type +datatype+ associated to this asset. 
     349  # Function: data_permission_profiles_for_current_user 
     350  # =================================================== 
     351  # 
     352  # Returns current user's data permission profiles in this asset for the given data type. 
    246353  # Return value contains at most one permission profile for each user group. 
    247354  # If the current user belongs to multiple user groups and through them has multiple 
     
    250357  # contains one permission profile which has all permissions the summed permission 
    251358  # profiles have. Thus returned permission profiles contain all permissions there 
    252   # are in the current user's permission profiles for data type +datatype+. 
    253   # +datatype+:: String specifying which type of data permission profiles is returned.  
    254   #              e.g. +datatype+ 'Report' gives report permission profiles etc. 
     359  # are in the current user's permission profiles for the given data type. 
     360  # 
     361  # Parameters: 
     362  # ----------- 
     363  # datatype - Specifies which type of data permission profiles is returned. 
     364  # 
    255365  def data_permission_profiles_for_current_user(datatype) 
    256366    return [] unless user = User.current_user 
     
    280390  end 
    281391 
     392  # Function: node_depth 
     393  # ==================== 
     394  # 
    282395  # Returns the number of nodes in the asset tree below the receiving asset. 
    283396  def node_depth 
     
    286399  end 
    287400 
     401  # Function: copy_permissions_from 
     402  # =============================== 
     403  # 
    288404  # Copies all permissions (i.e. AssetsUserGroups and their DataPermissionProfiles) 
    289405  # from Asset +asset+ to the receiver, whose existing permissions are destroyed. 
     
    294410  end 
    295411 
     412  # Function: copy_permissions_to_subassets 
     413  # ======================================= 
     414  # 
    296415  # Copies asset's permissions (i.e. AssetsUserGroups and their DataPermissionProfiles) to sub-assets. 
    297416  # Permissions are copied only to those assets the user is authorized to edit. 
     
    300419  # After call sub-assets will have the same permissions as the asset. 
    301420  # Returns the number of assets the permissions where copied to. 
    302   # +node_levels_down+:: Integer that defines how many nodes down the copying is done. Default is one node level down. 
    303   #                 e.g. If +node_levels_down+ is 2 then permissions are copied to asset's children and to their children. 
     421  # 
     422  # Parameters: 
     423  # ----------- 
     424  # node_levels_down - Integer that defines how many nodes down the copying is done. 
     425  #                    Default is one node level down. 
     426  #                    E.g. If node_levels_down is 2 then permissions are copied to 
     427  #                    asset's children and to their children. 
    304428  def copy_permissions_to_subassets(node_levels_down = 1, assets_groups = self.assets_user_groups) 
    305429    return 0 if node_levels_down < 1 
     
    313437  end 
    314438 
    315 protected 
    316  
    317   # Returns true if the user is authorized to perform operation +operation+ to this asset. 
    318   # +operation+::  Symbol defining which permission is checked. One of :edit, :create_report, 
    319   #                :create_work_order, :attach_file, :create_subasset, :edit_permissions, :remove. 
     439 
     440  # Function: authorized_to 
     441  # ======================= 
     442  # 
     443  # Returns true if the user is authorized to perform the given operation to this asset. 
     444  # 
     445  # Parameters: 
     446  # ----------- 
     447  # operation - Symbol defining which permission is checked. Possible operations 
     448  #             are :edit, :create_report, :create_work_order, :attach_file, 
     449  #             :create_subasset, :edit_permissions, :remove. 
    320450  def authorized_to(operation) 
    321451    return false unless User.current_user 
     
    325455  end 
    326456 
     457  # Function: after_find 
     458  # ==================== 
     459  # 
    327460  # Saves values of protected fields and use_parents_permissions to instance variables, 
    328461  # so that we can check if they have been changed. 
     
    333466  end 
    334467 
     468  # Function: check_authorized_to_edit_parent 
     469  # ========================================= 
     470  # 
    335471  # Prevents creation of parentless assets if root asset exists. Verifies that user is authorized to create new asset, which requires create_subasset permission to asset's parent. 
    336472  def check_authorized_to_edit_parent 
     
    351487  end 
    352488 
     489  # Function: check_authorized_to_edit 
     490  # ================================== 
     491  # 
    353492  # Verifies that user is authorized to edit this asset. 
    354493  def check_authorized_to_edit 
     
    381520  end 
    382521 
     522  # Function: check_if_root_or_parent_node 
     523  # ====================================== 
     524  # 
    383525  # Prevents deletion of the root node and parent nodes. 
    384526  def check_if_root_or_parent_node 
     
    387529  end 
    388530 
     531  # Function: check_use_parents_permissions 
     532  # ======================================= 
     533  # 
    389534  # Make sure parentless asset can't use parent's permissions. 
    390535  # Copies parent asset's permissions to asset if new asset is created with 
     
    399544  end 
    400545 
    401   # Copies AssetsUserGroups +assets_groups+ and all their data permission profiles 
     546  # Function: copy_permissions 
     547  # ========================== 
     548  # 
     549  # Copies given AssetsUserGroups and all their data permission profiles 
    402550  # to this asset if it is a new record or if user is authorized to edit this asset. 
    403551  # Asset's existing permissions will be destroyed. 
     
    405553  # If true +only_permission_profiles+ is given only data permission profiles will 
    406554  # be copied - use this only if you are sure that asset's user groups and their 
    407   # permissions are identical with those in +assets_groups+. 
     555  # permissions are identical with those in assets_groups. 
     556  # 
     557  # Parameters: 
     558  # ----------- 
     559  # assets_user_groups - AssetsUserGroups where the permissions are copied from. 
     560  # 
    408561  def copy_permissions(assets_groups) 
    409562    return false unless self.new_record? or self.authorized_to_edit_permissions? 
     
    425578  end 
    426579 
     580  # Function: information_unchanged? 
     581  # ================================ 
     582  # 
    427583  # Returns true if all attributes, except use_parents_permissions, are unchanged. 
    428584  def only_use_parents_permissions_changed? 
     
    435591  end 
    436592 
     593  # Function: use_parents_permissions_unchanged? 
     594  # ============================================ 
     595  # 
    437596  # Returns true if use_parents_permissions is unchanged. 
    438597  def use_parents_permissions_unchanged? 
     
    440599  end 
    441600 
     601 
     602  # Function: permissions_valid? 
     603  # ============================ 
     604  # 
     605  # Returns true if assets permissions are valid 
     606  def permissions_valid? 
     607    return true if AssetsUserGroup.find_by_asset_id_and_edit_permissions(self.id, true) 
     608    return false 
     609  end 
    442610end 
     611