mirror of
https://github.com/redmine/redmine.git
synced 2026-05-07 11:36:38 +02:00
User groups feature initial commit.
git-svn-id: http://redmine.rubyforge.org/svn/branches/work@1373 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
39
groups/app/models/group.rb
Normal file
39
groups/app/models/group.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
# redMine - project management software
|
||||
# Copyright (C) 2008 FreeCode
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
class Group < ActiveRecord::Base
|
||||
|
||||
has_many :users, :dependent => :nullify
|
||||
has_many :memberships, :class_name => 'Member', :as => :principal, :dependent => :destroy
|
||||
has_many :members, :as => :principal,
|
||||
:include => [ :project, :role ],
|
||||
:conditions => "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}",
|
||||
:order => "#{Project.table_name}.name"
|
||||
has_many :custom_values, :dependent => :delete_all, :as => :customized
|
||||
|
||||
validates_presence_of :name
|
||||
validates_uniqueness_of :name
|
||||
validates_length_of :name, :maximum => 30
|
||||
|
||||
def <=>(group)
|
||||
name <=> group.name
|
||||
end
|
||||
|
||||
def to_s
|
||||
name
|
||||
end
|
||||
end
|
||||
22
groups/app/models/group_custom_field.rb
Normal file
22
groups/app/models/group_custom_field.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
# redMine - project management software
|
||||
# Copyright (C) 2008 FreeCode
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
class GroupCustomField < CustomField
|
||||
def type_name
|
||||
:label_group_plural
|
||||
end
|
||||
end
|
||||
@@ -16,27 +16,52 @@
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
class Member < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
belongs_to :role
|
||||
belongs_to :project
|
||||
belongs_to :role
|
||||
belongs_to :principal, :polymorphic => true
|
||||
belongs_to :user, :foreign_key => 'principal_id'
|
||||
|
||||
validates_presence_of :role, :user, :project
|
||||
validates_uniqueness_of :user_id, :scope => :project_id
|
||||
|
||||
attr_protected :inherited_from
|
||||
|
||||
validates_presence_of :project, :role, :principal
|
||||
validates_uniqueness_of :principal_id, :scope => [:project_id, :principal_type, :inherited_from]
|
||||
|
||||
def validate
|
||||
errors.add :role_id, :activerecord_error_invalid if role && !role.member?
|
||||
end
|
||||
|
||||
def name
|
||||
self.user.name
|
||||
principal.name
|
||||
end
|
||||
|
||||
# Groups sorted by role then users sorted by role
|
||||
def <=>(member)
|
||||
role == member.role ? (user <=> member.user) : (role <=> member.role)
|
||||
principal_type == member.principal_type ?
|
||||
(role == member.role ? principal <=> member.principal : role <=> member.role) :
|
||||
(principal_type <=> member.principal_type)
|
||||
end
|
||||
|
||||
def to_s
|
||||
principal.to_s
|
||||
end
|
||||
|
||||
def after_save
|
||||
# Update memberships based on group inheritance
|
||||
if principal.is_a? Group
|
||||
Member.delete_all "inherited_from = #{id}"
|
||||
principal.users.each do |user|
|
||||
Member.create! :project => project, :role => role, :principal => user, :inherited_from => id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def before_destroy
|
||||
# remove category based auto assignments for this member
|
||||
IssueCategory.update_all "assigned_to_id = NULL", ["project_id = ? AND assigned_to_id = ?", project.id, user.id]
|
||||
# Remove inherited memberships
|
||||
Member.delete_all "inherited_from = #{id}"
|
||||
|
||||
# Remove category based auto assignments for this member
|
||||
if principal.is_a? User
|
||||
IssueCategory.update_all "assigned_to_id = NULL", ["project_id = ? AND assigned_to_id = ?", project_id, principal_id]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -20,8 +20,9 @@ class Project < ActiveRecord::Base
|
||||
STATUS_ACTIVE = 1
|
||||
STATUS_ARCHIVED = 9
|
||||
|
||||
has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}"
|
||||
has_many :users, :through => :members
|
||||
has_many :members, :include => :user, :conditions => "#{Member.table_name}.principal_type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}"
|
||||
has_many :memberships, :class_name => 'Member'
|
||||
has_many :users, :through => :members, :uniq => true
|
||||
has_many :custom_values, :dependent => :delete_all, :as => :customized
|
||||
has_many :enabled_modules, :dependent => :delete_all
|
||||
has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
|
||||
|
||||
@@ -35,18 +35,25 @@ class User < ActiveRecord::Base
|
||||
:username => '#{login}'
|
||||
}
|
||||
|
||||
has_many :memberships, :class_name => 'Member', :include => [ :project, :role ], :conditions => "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}", :order => "#{Project.table_name}.name", :dependent => :delete_all
|
||||
has_many :memberships, :class_name => 'Member',
|
||||
:as => :principal,
|
||||
:include => [ :project, :role ],
|
||||
:conditions => "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}",
|
||||
:order => "#{Project.table_name}.name, inherited_from ASC",
|
||||
:dependent => :delete_all
|
||||
|
||||
has_many :projects, :through => :memberships
|
||||
has_many :custom_values, :dependent => :delete_all, :as => :customized
|
||||
has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
|
||||
has_one :preference, :dependent => :destroy, :class_name => 'UserPreference'
|
||||
has_one :rss_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'"
|
||||
belongs_to :auth_source
|
||||
belongs_to :group
|
||||
|
||||
attr_accessor :password, :password_confirmation
|
||||
attr_accessor :last_before_login_on
|
||||
# Prevents unauthorized assignments
|
||||
attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
|
||||
attr_protected :login, :admin, :password, :password_confirmation, :hashed_password, :group_id
|
||||
|
||||
validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) }
|
||||
validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }
|
||||
@@ -71,6 +78,23 @@ class User < ActiveRecord::Base
|
||||
# update hashed_password if password was set
|
||||
self.hashed_password = User.hash_password(self.password) if self.password
|
||||
end
|
||||
|
||||
def after_save
|
||||
if @group_changed
|
||||
# Update inherited memberships
|
||||
Member.delete_all "principal_type = 'User' AND principal_id = #{id} AND inherited_from IS NOT NULL"
|
||||
unless group.nil?
|
||||
group.memberships.each do |m|
|
||||
Member.create! :project => m.project, :role => m.role, :principal => self, :inherited_from => m.id
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def group_id=(gid)
|
||||
@group_changed = true unless gid == group_id
|
||||
write_attribute(:group_id, gid)
|
||||
end
|
||||
|
||||
def self.active
|
||||
with_scope :find => { :conditions => [ "status = ?", STATUS_ACTIVE ] } do
|
||||
@@ -167,8 +191,8 @@ class User < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def notified_project_ids=(ids)
|
||||
Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id])
|
||||
Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty?
|
||||
Member.update_all("mail_notification = #{connection.quoted_false}", ["principal_type = 'User' AND principal_id = ?", id])
|
||||
Member.update_all("mail_notification = #{connection.quoted_true}", ["principal_type = 'User' AND principal_id = ? AND project_id IN (?)", id, ids]) if ids && !ids.empty?
|
||||
@notified_projects_ids = nil
|
||||
notified_projects_ids
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user