annotate .svn/pristine/c8/c8702ef29ff3c16a1b898e52dd1bebfb9737ece6.svn-base @ 1477:f2ad2199b49a bibplugin_integration

Close obsolete branch bibplugin_integration
author Chris Cannam
date Fri, 30 Nov 2012 14:41:31 +0000
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 class Member < ActiveRecord::Base
Chris@909 19 belongs_to :user
Chris@909 20 belongs_to :principal, :foreign_key => 'user_id'
Chris@909 21 has_many :member_roles, :dependent => :destroy
Chris@909 22 has_many :roles, :through => :member_roles
Chris@909 23 belongs_to :project
Chris@909 24
Chris@909 25 validates_presence_of :principal, :project
Chris@909 26 validates_uniqueness_of :user_id, :scope => :project_id
Chris@909 27
Chris@909 28 after_destroy :unwatch_from_permission_change
Chris@909 29
Chris@909 30 def name
Chris@909 31 self.user.name
Chris@909 32 end
Chris@909 33
Chris@909 34 alias :base_role_ids= :role_ids=
Chris@909 35 def role_ids=(arg)
Chris@909 36 ids = (arg || []).collect(&:to_i) - [0]
Chris@909 37 # Keep inherited roles
Chris@909 38 ids += member_roles.select {|mr| !mr.inherited_from.nil?}.collect(&:role_id)
Chris@909 39
Chris@909 40 new_role_ids = ids - role_ids
Chris@909 41 # Add new roles
Chris@909 42 new_role_ids.each {|id| member_roles << MemberRole.new(:role_id => id) }
Chris@909 43 # Remove roles (Rails' #role_ids= will not trigger MemberRole#on_destroy)
Chris@909 44 member_roles_to_destroy = member_roles.select {|mr| !ids.include?(mr.role_id)}
Chris@909 45 if member_roles_to_destroy.any?
Chris@909 46 member_roles_to_destroy.each(&:destroy)
Chris@909 47 unwatch_from_permission_change
Chris@909 48 end
Chris@909 49 end
Chris@909 50
Chris@909 51 def <=>(member)
Chris@909 52 a, b = roles.sort.first, member.roles.sort.first
Chris@909 53 a == b ? (principal <=> member.principal) : (a <=> b)
Chris@909 54 end
Chris@909 55
Chris@909 56 def deletable?
Chris@909 57 member_roles.detect {|mr| mr.inherited_from}.nil?
Chris@909 58 end
Chris@909 59
Chris@909 60 def include?(user)
Chris@909 61 if principal.is_a?(Group)
Chris@909 62 !user.nil? && user.groups.include?(principal)
Chris@909 63 else
Chris@909 64 self.user == user
Chris@909 65 end
Chris@909 66 end
Chris@909 67
Chris@909 68 def before_destroy
Chris@909 69 if user
Chris@909 70 # remove category based auto assignments for this member
Chris@909 71 IssueCategory.update_all "assigned_to_id = NULL", ["project_id = ? AND assigned_to_id = ?", project.id, user.id]
Chris@909 72 end
Chris@909 73 end
Chris@909 74
Chris@909 75 # Find or initilize a Member with an id, attributes, and for a Principal
Chris@909 76 def self.edit_membership(id, new_attributes, principal=nil)
Chris@909 77 @membership = id.present? ? Member.find(id) : Member.new(:principal => principal)
Chris@909 78 @membership.attributes = new_attributes
Chris@909 79 @membership
Chris@909 80 end
Chris@909 81
Chris@909 82 protected
Chris@909 83
Chris@909 84 def validate
Chris@909 85 errors.add_on_empty :role if member_roles.empty? && roles.empty?
Chris@909 86 end
Chris@909 87
Chris@909 88 private
Chris@909 89
Chris@909 90 # Unwatch things that the user is no longer allowed to view inside project
Chris@909 91 def unwatch_from_permission_change
Chris@909 92 if user
Chris@909 93 Watcher.prune(:user => user, :project => project)
Chris@909 94 end
Chris@909 95 end
Chris@909 96 end