Changeset 357
- Timestamp:
- 12/18/06 16:54:39 (2 years ago)
- Files:
-
- branches/1.0/app/controllers/application.rb (modified) (6 diffs)
- branches/1.0/app/controllers/asset_controller.rb (modified) (2 diffs)
- branches/1.0/app/controllers/asset_permission_controller.rb (modified) (5 diffs)
- branches/1.0/app/controllers/data_permission_controller.rb (modified) (1 diff)
- branches/1.0/app/controllers/message_controller.rb (modified) (1 diff)
- branches/1.0/app/controllers/report_controller.rb (modified) (1 diff)
- branches/1.0/app/controllers/report_type_controller.rb (modified) (2 diffs)
- branches/1.0/app/controllers/user_controller.rb (modified) (4 diffs)
- branches/1.0/app/controllers/work_order_controller.rb (modified) (1 diff)
- branches/1.0/app/helpers/application_helper.rb (modified) (4 diffs)
- branches/1.0/app/models/asset.rb (modified) (39 diffs)
- branches/1.0/app/models/assets_user_group.rb (modified) (4 diffs)
- branches/1.0/app/models/report.rb (modified) (3 diffs)
- branches/1.0/app/models/report_field.rb (modified) (2 diffs)
- branches/1.0/app/views/asset_tree/_asset_tree.rhtml (modified) (1 diff)
- branches/1.0/app/views/asset_tree/_asset_types.rhtml (modified) (1 diff)
- branches/1.0/app/views/asset_tree/_recursive_tree_view.rhtml (deleted)
- branches/1.0/app/views/layouts/mainlevel.rhtml (modified) (2 diffs)
- branches/1.0/app/views/report/search.rhtml (modified) (1 diff)
- branches/1.0/app/views/report_type/create_step4.rhtml (modified) (2 diffs)
- branches/1.0/app/views/shared/_list_items.rhtml (modified) (5 diffs)
- branches/1.0/app/views/timeline/_display_event.rhtml (modified) (2 diffs)
- branches/1.0/app/views/user/edit.rhtml (modified) (1 diff)
- branches/1.0/app/views/work_order/_display_work_order_event.rhtml (modified) (1 diff)
- branches/1.0/app/views/work_order/_form.rhtml (modified) (2 diffs)
- branches/1.0/app/views/work_order/create.rhtml (modified) (1 diff)
- branches/1.0/app/views/work_order/view.rhtml (modified) (1 diff)
- branches/1.0/config/database_independent_sql.rb (modified) (2 diffs)
- branches/1.0/config/menu.rb (modified) (4 diffs)
- branches/1.0/db/migrate/001_initial.rb (modified) (2 diffs)
- branches/1.0/db/migrate/002_initial_data.rb (modified) (2 diffs)
- branches/1.0/lib/menu_helper.rb (modified) (1 diff)
- branches/1.0/public/javascripts/application.js (modified) (1 diff)
- branches/1.0/public/stylesheets/mainlevel.css (modified) (1 diff)
- branches/1.0/test/fixtures/action_keys.yml (modified) (1 diff)
- branches/1.0/test/fixtures/action_keys_keyrings.yml (modified) (1 diff)
- branches/1.0/test/functional/asset_controller_test.rb (modified) (1 diff)
- branches/1.0/test/functional/asset_permission_controller_test.rb (modified) (7 diffs)
- branches/1.0/test/functional/user_controller_test.rb (modified) (2 diffs)
- branches/1.0/test/functional/user_group_controller_test.rb (modified) (1 diff)
- branches/1.0/test/unit/asset_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.0/app/controllers/application.rb
r326 r357 9 9 10 10 # Class: ApplicationController 11 # ============================ =====11 # ============================ 12 12 # 13 13 # The filters added to this controller will be run for all controllers in the application. … … 206 206 flash[:notice] = msg_create_successful(@model) 207 207 redirect_to(redirect) and return 208 els e208 elsif @model.errors.empty? 209 209 flash[:error] = msg_system_failure_in(_('Creation')) 210 210 end … … 233 233 flash[:notice] = msg_changes_saved 234 234 redirect_to(redirect) and return 235 els e235 elsif @model.errors.empty? 236 236 flash[:notice] = msg_saving_failed 237 237 end … … 284 284 } 285 285 286 flash[:notice] = _("Removed %d %s", num_instances_deleted, _(Localization.human_readable_model_name(model_class).pluralize)) if num_instances_deleted > 1286 flash[:notice] = _("Removed %d %s", num_instances_deleted, _(Localization.human_readable_model_name(model_class).pluralize)) if num_instances_deleted > 0 287 287 end 288 288 … … 325 325 end 326 326 327 # Function: find_selected_assets 328 # ============================== 329 # 330 # Finds selected assets and constructs conditions for further database queries 331 # from selected assets. 327 332 def find_selected_assets 328 333 if session[:select_branches] 329 334 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])] 331 336 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 335 339 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) 338 344 else 339 345 @selected_assets = [@selected_asset] … … 364 370 if session[:view_as_list] 365 371 @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 else370 assets = @selected_asset.children371 end372 372 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) 381 378 end 382 379 } 383 380 else 384 381 @open_node_list = session[:open_tree_node_list] 385 @nodes = Asset.find(:all, :readonly => 'true', :conditions => 'parent_id is null')386 382 end 387 383 end branches/1.0/app/controllers/asset_controller.rb
r318 r357 60 60 @new_asset.parent = @selected_asset 61 61 @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 68 70 end 69 71 … … 105 107 106 108 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? 108 110 return 109 111 end branches/1.0/app/controllers/asset_permission_controller.rb
r305 r357 33 33 34 34 @title = _('User groups that have permissions to asset %s', @selected_asset.code_and_name) 35 @fields = [ 35 @fields = [ 36 36 [_('Name'), :name], 37 [_('Information'), :information] 37 [_('Information'), :information] 38 38 ] 39 39 @items = @selected_asset.user_groups … … 93 93 remove_count = asset.remove_user_groups(params['delete'].keys) 94 94 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 95 100 if remove_count > 0 96 101 flash[:notice] = _('Removed this asset from %d user groups.', remove_count) … … 108 113 109 114 if @selected_asset.update_attribute(:use_parents_permissions, params[:selected_asset][:use_parents_permissions]) 110 flash[:notice] = msg_changes_saved 111 els e115 flash[:notice] = msg_changes_saved 116 elsif @selected_asset.errors.empty? 112 117 flash[:error] = msg_saving_failed 113 118 end … … 175 180 176 181 # 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) 188 195 end 189 196 … … 194 201 def update_permission_profiles 195 202 invalid_request and return unless request.post? 196 203 197 204 # find object and check that authorized to edit and that asset it belongs to isn't using parent's permissions. 198 205 assets_user_group = AssetsUserGroup.find(params[:id]) branches/1.0/app/controllers/data_permission_controller.rb
r305 r357 92 92 if @object.update_permissions(params[:permission], params[:object]) 93 93 flash[:notice] = msg_changes_saved 94 els e94 elsif @object.errors.empty? 95 95 flash[:error] = msg_saving_failed 96 96 end branches/1.0/app/controllers/message_controller.rb
r305 r357 38 38 if @message.unread 39 39 @message.unread = false 40 unless @message.save 40 unless @message.save and @message.errors.empty? 41 41 flash[:error] = msg_saving_failed 42 42 end branches/1.0/app/controllers/report_controller.rb
r326 r357 150 150 @report.editable = params[:report][:editable] if params[:report] and @report.report_type.editable 151 151 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 157 153 flash[:notice] = msg_changes_saved 158 154 redirect_to :action => 'view', :id => @report.id and return branches/1.0/app/controllers/report_type_controller.rb
r305 r357 165 165 if request.post? 166 166 @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 168 169 if @new_field_group.valid? 169 170 @report_type.report_field_groups << @new_field_group … … 235 236 @groups = @report_type.report_field_groups 236 237 @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] } 238 240 @new_field_group = ReportFieldGroup.new 239 241 end branches/1.0/app/controllers/user_controller.rb
r326 r357 75 75 [_('Description'), :description] 76 76 ] 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'} ] 80 80 ] 81 81 @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 83 92 84 93 if request.post? … … 88 97 flash[:notice] = _('User information changed') 89 98 info "User information changed for #{@model.login}" 90 els e99 elsif @model.errors.empty? 91 100 flash[:error] = _('Unable to change user information') 92 101 end … … 98 107 99 108 # Ignore GET requests 100 return if request.get? 109 return if request.get? or not params[:delete] or params[:delete].empty? 101 110 102 111 names = [] … … 107 116 end 108 117 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(',')) 109 133 end 110 134 branches/1.0/app/controllers/work_order_controller.rb
r318 r357 105 105 @users = @work_order.asset.users(@work_order.responsible_user_id) 106 106 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) 108 108 109 109 if request.post? branches/1.0/app/helpers/application_helper.rb
r318 r357 103 103 last_year = options[:first_year] + options[:years] 104 104 options[:include_time] = false unless options.has_key?(:include_time) 105 id_prefix = prefix.gsub('][', '_').gsub( /\[|\]/, '_')105 id_prefix = prefix.gsub('][', '_').gsub('[', '_').gsub(/\]$/, '') 106 106 107 107 select = select_tag("#{prefix}[year]", year_options(time.year, options[:first_year], last_year), {'id' => "#{id_prefix}_year"}) + … … 543 543 # Function: link_class_for_asset_tree_node 544 544 # ======================================== 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 == node545 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 549 549 if is_selected_type or not session[:select_branches] 550 550 return 'asset_tree_selected_asset' … … 566 566 # ======================================== 567 567 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" 569 569 end 570 570 571 571 # Function: cancel_tag 572 # ==================== ===572 # ==================== 573 573 # 574 574 # Retuns a cancellation submit button for forms. … … 640 640 table + end_form_tag 641 641 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 642 683 end 643 branches/1.0/app/models/asset.rb
r105 r357 2 2 # See license agreement for additional rights 3 3 4 # Class: Asset 5 # ============ 6 # 4 7 class Asset < ProtectorModel 5 8 # Protection … … 26 29 acts_as_tree :order => 'code' 27 30 31 # Function: Asset.minimal_select 32 # ============================== 33 # 28 34 # Returns the smallest possible SELECT clause that can be used in a find call for this model. 29 35 # Contains protected fields and +use_parents_permissions+. … … 32 38 end 33 39 40 # Function: Asset.full_code_for 41 # ============================= 42 # 34 43 # Returns the full code for asset with the given id or nil 44 # 45 # Parameters: 46 # ----------- 47 # id - Asset id 48 # 35 49 def Asset.full_code_for(id) 36 50 begin … … 41 55 end 42 56 57 # Function: Asset.get_first_authorized_to_read 58 # ============================================ 59 # 43 60 # Returns the first found asset user is authorized to read. 44 61 # If user is unauthorized to read any assets raises an exception. … … 49 66 end 50 67 68 # Function: Asset.find_by_full_code 69 # ================================= 70 # 51 71 # Finds assets user is authorized to read by the full code. 72 # 73 # Parameters: 74 # ----------- 75 # full_code - Full code of an asset 76 # 52 77 def Asset.find_by_full_code(full_code) 53 78 code = full_code.split('-')[-1] … … 58 83 end 59 84 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 # 63 96 def Asset.find_authorized_to(operation, args) 64 97 return records_not_found(args) unless User.current_user … … 84 117 end 85 118 119 # Function: full_code 120 # =================== 121 # 86 122 # Returns the asset's full code. 87 123 # 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 # 90 126 def full_code 91 127 return self.code_prefix + self.code 92 128 end 93 129 130 # Function: code_prefix 131 # ===================== 132 # 94 133 # Returns the asset's code prefix. 95 134 # … … 104 143 end 105 144 106 # Returns: 'code (name)' 145 # Function: code_and_name 146 # ======================= 147 # 148 # Returns code and name in from 'code (name)' 107 149 def code_and_name 108 150 self.code + ' (' + self.name + ')' 109 151 end 110 152 153 # Function: is_root? 154 # 111 155 # Returns true if asset is the root asset. 112 156 def is_root? … … 114 158 end 115 159 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. 117 164 # i.e. returns asset's id and it's children's ids and their chilren's ids etc. 118 165 def id_and_subasset_ids(levels = nil) … … 124 171 end 125 172 173 # Function: path_to_root 174 # ====================== 175 # 126 176 # Returns assets that are in the path from this asset to the root asset. 127 177 def path_to_root … … 130 180 end 131 181 182 # Function: users 183 # =============== 184 # 132 185 # 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 # 134 191 def users(exclude_user_id = nil) 135 192 return [] if self.user_groups.empty? … … 140 197 end 141 198 199 # Function: authorized_to_read? 200 # ============================= 201 # 142 202 # Returns true if the user is authorized to read this asset. 143 203 def authorized_to_read? … … 147 207 end 148 208 209 # Function: authorized_to_create_report? 210 # ====================================== 211 # 149 212 # Returns true if the user is authorized to create reports to this asset 150 213 def authorized_to_create_report? … … 152 215 end 153 216 217 # Function: authorized_to_create_work_order? 218 # ========================================== 219 # 154 220 # Returns true if the user is authorized to create work_orders to this asset 155 221 def authorized_to_create_work_order? … … 157 223 end 158 224 225 # Function: authorized_to_attach_file? 226 # ==================================== 227 # 159 228 # Returns true if the user is authorized to create attachments to this asset 160 229 def authorized_to_attach_file? … … 162 231 end 163 232 233 # Function: authorized_to_create_subasset? 234 # ======================================== 235 # 164 236 # Returns true if the user is authorized to create sub-assets to this asset 165 237 def authorized_to_create_subasset? … … 167 239 end 168 240 241 # Function: authorized_to_edit_permissions? 242 # ========================================= 243 # 169 244 # Returns true if the user is authorized to edit asset permissions for this asset 170 245 def authorized_to_edit_permissions? … … 172 247 end 173 248 249 # Function: authorized_to_remove? 250 # =============================== 251 # 174 252 # Returns true if the user is authorized to remove this asset 175 253 def authorized_to_remove? … … 177 255 end 178 256 257 # Function: authorized_to_create? 258 # =============================== 259 # 179 260 # Returns true if the user is authorized to create new instances of +datatype+ to this asset 180 261 def authorized_to_create?(datatype) … … 191 272 end 192 273 274 # Function: has_subasset_authorized_to_read? 275 # ========================================== 276 # 193 277 # Returns true if asset has a sub-asset user is authorized to read. 194 278 def has_subasset_authorized_to_read? … … 200 284 end 201 285 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. 205 291 # The same association is created also for all sub-assets using this asset's permissions. 206 292 # 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 # 207 300 def add_user_group(user_group, permissions = {}) 208 301 raise("#{user_group.class} in not a UserGroup") unless user_group.is_a?(UserGroup) … … 222 315 end 223 316 317 # Function: remove_user_groups 318 # ============================ 319 # 224 320 # Removes user groups with ids +user_group_ids+ from this asset i.e. removes 225 321 # 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. 227 324 def remove_user_groups(user_group_ids) 228 325 raise('Unable to remove user groups without ids') if user_group_ids.empty? … … 231 328 associations_to_destroy = self.assets_user_groups.find(:all, :conditions => ['user_group_id IN (?)', user_group_ids]) 232 329 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? 240 341 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 242 346 return remove_count 243 347 end 244 348 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. 246 353 # Return value contains at most one permission profile for each user group. 247 354 # If the current user belongs to multiple user groups and through them has multiple … … 250 357 # contains one permission profile which has all permissions the summed permission 251 358 # 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 # 255 365 def data_permission_profiles_for_current_user(datatype) 256 366 return [] unless user = User.current_user … … 280 390 end 281 391 392 # Function: node_depth 393 # ==================== 394 # 282 395 # Returns the number of nodes in the asset tree below the receiving asset. 283 396 def node_depth … … 286 399 end 287 400 401 # Function: copy_permissions_from 402 # =============================== 403 # 288 404 # Copies all permissions (i.e. AssetsUserGroups and their DataPermissionProfiles) 289 405 # from Asset +asset+ to the receiver, whose existing permissions are destroyed. … … 294 410 end 295 411 412 # Function: copy_permissions_to_subassets 413 # ======================================= 414 # 296 415 # Copies asset's permissions (i.e. AssetsUserGroups and their DataPermissionProfiles) to sub-assets. 297 416 # Permissions are copied only to those assets the user is authorized to edit. … … 300 419 # After call sub-assets will have the same permissions as the asset. 301 420 # 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. 304 428 def copy_permissions_to_subassets(node_levels_down = 1, assets_groups = self.assets_user_groups) 305 429 return 0 if node_levels_down < 1 … … 313 437 end 314 438 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. 320 450 def authorized_to(operation) 321 451 return false unless User.current_user … … 325 455 end 326 456 457 # Function: after_find 458 # ==================== 459 # 327 460 # Saves values of protected fields and use_parents_permissions to instance variables, 328 461 # so that we can check if they have been changed. … … 333 466 end 334 467 468 # Function: check_authorized_to_edit_parent 469 # ========================================= 470 # 335 471 # 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. 336 472 def check_authorized_to_edit_parent … … 351 487 end 352 488 489 # Function: check_authorized_to_edit 490 # ================================== 491 # 353 492 # Verifies that user is authorized to edit this asset. 354 493 def check_authorized_to_edit … … 381 520 end 382 521 522 # Function: check_if_root_or_parent_node 523 # ====================================== 524 # 383 525 # Prevents deletion of the root node and parent nodes. 384 526 def check_if_root_or_parent_node … … 387 529 end 388 530 531 # Function: check_use_parents_permissions 532 # ======================================= 533 # 389 534 # Make sure parentless asset can't use parent's permissions. 390 535 # Copies parent asset's permissions to asset if new asset is created with … … 399 544 end 400 545 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 402 550 # to this asset if it is a new record or if user is authorized to edit this asset. 403 551 # Asset's existing permissions will be destroyed. … … 405 553 # If true +only_permission_profiles+ is given only data permission profiles will 406 554 # 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 # 408 561 def copy_permissions(assets_groups) 409 562 return false unless self.new_record? or self.authorized_to_edit_permissions? … … 425 578 end 426 579 580 # Function: information_unchanged? 581 # ================================ 582 # 427 583 # Returns true if all attributes, except use_parents_permissions, are unchanged. 428 584 def only_use_parents_permissions_changed? … … 435 591 end 436 592 593 # Function: use_parents_permissions_unchanged? 594 # ============================================ 595 # 437 596 # Returns true if use_parents_permissions is unchanged. 438 597 def use_parents_permissions_unchanged? … … 440 599 end 441 600 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 442 610 end 611