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 / .svn / pristine / 48 / 4881b5ad6e4efa4165188246776dbe492c56e6df.svn-base @ 1298:4f746d8966dd

History | View | Annotate | Download (3.83 KB)

1 1295:622f24f53b42 Chris
# Redmine - project management software
2
# Copyright (C) 2006-2013  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 Principal < ActiveRecord::Base
19
  self.table_name = "#{table_name_prefix}users#{table_name_suffix}"
20
21
  # Account statuses
22
  STATUS_ANONYMOUS  = 0
23
  STATUS_ACTIVE     = 1
24
  STATUS_REGISTERED = 2
25
  STATUS_LOCKED     = 3
26
27
  has_many :members, :foreign_key => 'user_id', :dependent => :destroy
28
  has_many :memberships, :class_name => 'Member', :foreign_key => 'user_id', :include => [ :project, :roles ], :conditions => "#{Project.table_name}.status<>#{Project::STATUS_ARCHIVED}", :order => "#{Project.table_name}.name"
29
  has_many :projects, :through => :memberships
30
  has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
31
32
  # Groups and active users
33
  scope :active, lambda { where(:status => STATUS_ACTIVE) }
34
35
  scope :like, lambda {|q|
36
    q = q.to_s
37
    if q.blank?
38
      where({})
39
    else
40
      pattern = "%#{q}%"
41
      sql = %w(login firstname lastname mail).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ")
42
      params = {:p => pattern}
43
      if q =~ /^(.+)\s+(.+)$/
44
        a, b = "#{$1}%", "#{$2}%"
45
        sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:a) AND LOWER(#{table_name}.lastname) LIKE LOWER(:b))"
46
        sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))"
47
        params.merge!(:a => a, :b => b)
48
      end
49
      where(sql, params)
50
    end
51
  }
52
53
  # Principals that are members of a collection of projects
54
  scope :member_of, lambda {|projects|
55
    projects = [projects] unless projects.is_a?(Array)
56
    if projects.empty?
57
      where("1=0")
58
    else
59
      ids = projects.map(&:id)
60
      active.uniq.joins(:members).where("#{Member.table_name}.project_id IN (?)", ids)
61
    end
62
  }
63
  # Principals that are not members of projects
64
  scope :not_member_of, lambda {|projects|
65
    projects = [projects] unless projects.is_a?(Array)
66
    if projects.empty?
67
      where("1=0")
68
    else
69
      ids = projects.map(&:id)
70
      where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
71
    end
72
  }
73
  scope :sorted, lambda { order(*Principal.fields_for_order_statement)}
74
75
  before_create :set_default_empty_values
76
77
  def name(formatter = nil)
78
    to_s
79
  end
80
81
  def <=>(principal)
82
    if principal.nil?
83
      -1
84
    elsif self.class.name == principal.class.name
85
      self.to_s.downcase <=> principal.to_s.downcase
86
    else
87
      # groups after users
88
      principal.class.name <=> self.class.name
89
    end
90
  end
91
92
  # Returns an array of fields names than can be used to make an order statement for principals.
93
  # Users are sorted before Groups.
94
  # Examples:
95
  def self.fields_for_order_statement(table=nil)
96
    table ||= table_name
97
    columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id']
98
    columns.uniq.map {|field| "#{table}.#{field}"}
99
  end
100
101
  protected
102
103
  # Make sure we don't try to insert NULL values (see #4632)
104
  def set_default_empty_values
105
    self.login ||= ''
106
    self.hashed_password ||= ''
107
    self.firstname ||= ''
108
    self.lastname ||= ''
109
    self.mail ||= ''
110
    true
111
  end
112
end