To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / member.rb @ 956:fa2a1b6cda26

History | View | Annotate | Download (3.06 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
class Member < ActiveRecord::Base
19
  belongs_to :user
20
  belongs_to :principal, :foreign_key => 'user_id'
21
  has_many :member_roles, :dependent => :destroy
22
  has_many :roles, :through => :member_roles
23
  belongs_to :project
24

    
25
  validates_presence_of :principal, :project
26
  validates_uniqueness_of :user_id, :scope => :project_id
27

    
28
  after_destroy :unwatch_from_permission_change
29

    
30
  def name
31
    self.user.name
32
  end
33

    
34
  alias :base_role_ids= :role_ids=
35
  def role_ids=(arg)
36
    ids = (arg || []).collect(&:to_i) - [0]
37
    # Keep inherited roles
38
    ids += member_roles.select {|mr| !mr.inherited_from.nil?}.collect(&:role_id)
39

    
40
    new_role_ids = ids - role_ids
41
    # Add new roles
42
    new_role_ids.each {|id| member_roles << MemberRole.new(:role_id => id) }
43
    # Remove roles (Rails' #role_ids= will not trigger MemberRole#on_destroy)
44
    member_roles_to_destroy = member_roles.select {|mr| !ids.include?(mr.role_id)}
45
    if member_roles_to_destroy.any?
46
      member_roles_to_destroy.each(&:destroy)
47
      unwatch_from_permission_change
48
    end
49
  end
50

    
51
  def <=>(member)
52
    a, b = roles.sort.first, member.roles.sort.first
53
    if a == b
54
      if principal
55
        principal <=> member.principal
56
      else
57
        1
58
      end
59
    elsif a
60
      a <=> b
61
    else
62
      1
63
    end
64
  end
65

    
66
  def deletable?
67
    member_roles.detect {|mr| mr.inherited_from}.nil?
68
  end
69

    
70
  def include?(user)
71
    if principal.is_a?(Group)
72
      !user.nil? && user.groups.include?(principal)
73
    else
74
      self.user == user
75
    end
76
  end
77

    
78
  def before_destroy
79
    if user
80
      # remove category based auto assignments for this member
81
      IssueCategory.update_all "assigned_to_id = NULL", ["project_id = ? AND assigned_to_id = ?", project.id, user.id]
82
    end
83
  end
84

    
85
  # Find or initilize a Member with an id, attributes, and for a Principal
86
  def self.edit_membership(id, new_attributes, principal=nil)
87
    @membership = id.present? ? Member.find(id) : Member.new(:principal => principal)
88
    @membership.attributes = new_attributes
89
    @membership
90
  end
91

    
92
  protected
93

    
94
  def validate
95
    errors.add_on_empty :role if member_roles.empty? && roles.empty?
96
  end
97

    
98
  private
99

    
100
  # Unwatch things that the user is no longer allowed to view inside project
101
  def unwatch_from_permission_change
102
    if user
103
      Watcher.prune(:user => user, :project => project)
104
    end
105
  end
106
end