Adds an issues visibility level on roles (#7412).
It can be set so that users only see their own issues (created or assigned). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5416 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
# redMine - project management software
|
||||
# Copyright (C) 2006-2007 Jean-Philippe Lang
|
||||
# Redmine - project management software
|
||||
# Copyright (C) 2006-2011 Jean-Philippe Lang
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
@@ -221,6 +221,10 @@ class ApplicationController < ActionController::Base
|
||||
def find_issues
|
||||
@issues = Issue.find_all_by_id(params[:id] || params[:ids])
|
||||
raise ActiveRecord::RecordNotFound if @issues.empty?
|
||||
if @issues.detect {|issue| !issue.visible?}
|
||||
deny_access
|
||||
return
|
||||
end
|
||||
@projects = @issues.collect(&:project).compact.uniq
|
||||
@project = @projects.first if @projects.size == 1
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Redmine - project management software
|
||||
# Copyright (C) 2006-2008 Jean-Philippe Lang
|
||||
# Copyright (C) 2006-2011 Jean-Philippe Lang
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
@@ -251,7 +251,13 @@ class IssuesController < ApplicationController
|
||||
|
||||
private
|
||||
def find_issue
|
||||
# Issue.visible.find(...) can not be used to redirect user to the login form
|
||||
# if the issue actually exists but requires authentication
|
||||
@issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
|
||||
unless @issue.visible?
|
||||
deny_access
|
||||
return
|
||||
end
|
||||
@project = @issue.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
|
||||
+20
-2
@@ -88,12 +88,30 @@ class Issue < ActiveRecord::Base
|
||||
|
||||
# Returns a SQL conditions string used to find all issues visible by the specified user
|
||||
def self.visible_condition(user, options={})
|
||||
Project.allowed_to_condition(user, :view_issues, options)
|
||||
Project.allowed_to_condition(user, :view_issues, options) do |role, user|
|
||||
case role.issues_visibility
|
||||
when 'default'
|
||||
nil
|
||||
when 'own'
|
||||
"(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id = #{user.id})"
|
||||
else
|
||||
'1=0'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if usr or current user is allowed to view the issue
|
||||
def visible?(usr=nil)
|
||||
(usr || User.current).allowed_to?(:view_issues, self.project)
|
||||
(usr || User.current).allowed_to?(:view_issues, self.project) do |role, user|
|
||||
case role.issues_visibility
|
||||
when 'default'
|
||||
true
|
||||
when 'own'
|
||||
self.author == user || self.assigned_to == user
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def after_initialize
|
||||
|
||||
@@ -174,6 +174,13 @@ class Project < ActiveRecord::Base
|
||||
if statement_by_role.empty?
|
||||
"1=0"
|
||||
else
|
||||
if block_given?
|
||||
statement_by_role.each do |role, statement|
|
||||
if s = yield(role, user)
|
||||
statement_by_role[role] = "(#{statement} AND (#{s}))"
|
||||
end
|
||||
end
|
||||
end
|
||||
"((#{base_statement}) AND (#{statement_by_role.values.join(' OR ')}))"
|
||||
end
|
||||
end
|
||||
|
||||
+9
-1
@@ -19,6 +19,11 @@ class Role < ActiveRecord::Base
|
||||
# Built-in roles
|
||||
BUILTIN_NON_MEMBER = 1
|
||||
BUILTIN_ANONYMOUS = 2
|
||||
|
||||
ISSUES_VISIBILITY_OPTIONS = [
|
||||
['default', :label_issues_visibility_all],
|
||||
['own', :label_issues_visibility_own]
|
||||
]
|
||||
|
||||
named_scope :givable, { :conditions => "builtin = 0", :order => 'position' }
|
||||
named_scope :builtin, lambda { |*args|
|
||||
@@ -43,7 +48,10 @@ class Role < ActiveRecord::Base
|
||||
validates_presence_of :name
|
||||
validates_uniqueness_of :name
|
||||
validates_length_of :name, :maximum => 30
|
||||
|
||||
validates_inclusion_of :issues_visibility,
|
||||
:in => ISSUES_VISIBILITY_OPTIONS.collect(&:first),
|
||||
:if => lambda {|role| role.respond_to?(:issues_visibility)}
|
||||
|
||||
def permissions
|
||||
read_attribute(:permissions) || []
|
||||
end
|
||||
|
||||
+15
-8
@@ -394,10 +394,10 @@ class User < Principal
|
||||
# * a permission Symbol (eg. :edit_project)
|
||||
# Context can be:
|
||||
# * a project : returns true if user is allowed to do the specified action on this project
|
||||
# * a group of projects : returns true if user is allowed on every project
|
||||
# * an array of projects : returns true if user is allowed on every project
|
||||
# * nil with options[:global] set : check if user has at least one role allowed for this action,
|
||||
# or falls back to Non Member / Anonymous permissions depending if the user is logged
|
||||
def allowed_to?(action, context, options={})
|
||||
def allowed_to?(action, context, options={}, &block)
|
||||
if context && context.is_a?(Project)
|
||||
# No action allowed on archived projects
|
||||
return false unless context.active?
|
||||
@@ -408,12 +408,15 @@ class User < Principal
|
||||
|
||||
roles = roles_for_project(context)
|
||||
return false unless roles
|
||||
roles.detect {|role| (context.is_public? || role.member?) && role.allowed_to?(action)}
|
||||
|
||||
roles.detect {|role|
|
||||
(context.is_public? || role.member?) &&
|
||||
role.allowed_to?(action) &&
|
||||
(block_given? ? yield(role, self) : true)
|
||||
}
|
||||
elsif context && context.is_a?(Array)
|
||||
# Authorize if user is authorized on every element of the array
|
||||
context.map do |project|
|
||||
allowed_to?(action,project,options)
|
||||
allowed_to?(action, project, options, &block)
|
||||
end.inject do |memo,allowed|
|
||||
memo && allowed
|
||||
end
|
||||
@@ -423,7 +426,11 @@ class User < Principal
|
||||
|
||||
# authorize if user has at least one role that has this permission
|
||||
roles = memberships.collect {|m| m.roles}.flatten.uniq
|
||||
roles.detect {|r| r.allowed_to?(action)} || (self.logged? ? Role.non_member.allowed_to?(action) : Role.anonymous.allowed_to?(action))
|
||||
roles << (self.logged? ? Role.non_member : Role.anonymous)
|
||||
roles.detect {|role|
|
||||
role.allowed_to?(action) &&
|
||||
(block_given? ? yield(role, self) : true)
|
||||
}
|
||||
else
|
||||
false
|
||||
end
|
||||
@@ -431,8 +438,8 @@ class User < Principal
|
||||
|
||||
# Is the user allowed to do the specified action on any project?
|
||||
# See allowed_to? for the actions and valid options.
|
||||
def allowed_to_globally?(action, options)
|
||||
allowed_to?(action, nil, options.reverse_merge(:global => true))
|
||||
def allowed_to_globally?(action, options, &block)
|
||||
allowed_to?(action, nil, options.reverse_merge(:global => true), &block)
|
||||
end
|
||||
|
||||
safe_attributes 'login',
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
<%= error_messages_for 'role' %>
|
||||
|
||||
<% unless @role.builtin? %>
|
||||
<div class="box">
|
||||
<% unless @role.builtin? %>
|
||||
<p><%= f.text_field :name, :required => true %></p>
|
||||
<p><%= f.check_box :assignable %></p>
|
||||
<% end %>
|
||||
<p><%= f.select :issues_visibility, Role::ISSUES_VISIBILITY_OPTIONS.collect {|v| [l(v.last), v.first]} %></p>
|
||||
<% if @role.new_record? && @roles.any? %>
|
||||
<p><label><%= l(:label_copy_workflow_from) %></label>
|
||||
<%= select_tag(:copy_workflow_from, content_tag("option") + options_from_collection_for_select(@roles, :id, :name)) %></p>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<h3><%= l(:label_permissions) %></h3>
|
||||
<div class="box" id="permissions">
|
||||
|
||||
Reference in New Issue
Block a user