Revision 1298:4f746d8966dd .svn/pristine/06
| .svn/pristine/06/061fdfd5800e4aa71d26012579cbbe6954d78738.svn-base | ||
|---|---|---|
| 1 |
# $Id: testem.rb 121 2006-05-15 18:36:24Z blackhedd $ |
|
| 2 |
# |
|
| 3 |
# |
|
| 4 |
|
|
| 5 |
require 'test/unit' |
|
| 6 |
require 'tests/testber' |
|
| 7 |
require 'tests/testldif' |
|
| 8 |
require 'tests/testldap' |
|
| 9 |
require 'tests/testpsw' |
|
| 10 |
require 'tests/testfilter' |
|
| 11 |
|
|
| 12 |
|
|
| .svn/pristine/06/0661103167f6b9ecf574c505f5bd6a17eadf893e.svn-base | ||
|---|---|---|
| 1 |
# 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 |
require "digest/sha1" |
|
| 19 |
|
|
| 20 |
class User < Principal |
|
| 21 |
include Redmine::SafeAttributes |
|
| 22 |
|
|
| 23 |
# Different ways of displaying/sorting users |
|
| 24 |
USER_FORMATS = {
|
|
| 25 |
:firstname_lastname => {
|
|
| 26 |
:string => '#{firstname} #{lastname}',
|
|
| 27 |
:order => %w(firstname lastname id), |
|
| 28 |
:setting_order => 1 |
|
| 29 |
}, |
|
| 30 |
:firstname_lastinitial => {
|
|
| 31 |
:string => '#{firstname} #{lastname.to_s.chars.first}.',
|
|
| 32 |
:order => %w(firstname lastname id), |
|
| 33 |
:setting_order => 2 |
|
| 34 |
}, |
|
| 35 |
:firstname => {
|
|
| 36 |
:string => '#{firstname}',
|
|
| 37 |
:order => %w(firstname id), |
|
| 38 |
:setting_order => 3 |
|
| 39 |
}, |
|
| 40 |
:lastname_firstname => {
|
|
| 41 |
:string => '#{lastname} #{firstname}',
|
|
| 42 |
:order => %w(lastname firstname id), |
|
| 43 |
:setting_order => 4 |
|
| 44 |
}, |
|
| 45 |
:lastname_coma_firstname => {
|
|
| 46 |
:string => '#{lastname}, #{firstname}',
|
|
| 47 |
:order => %w(lastname firstname id), |
|
| 48 |
:setting_order => 5 |
|
| 49 |
}, |
|
| 50 |
:lastname => {
|
|
| 51 |
:string => '#{lastname}',
|
|
| 52 |
:order => %w(lastname id), |
|
| 53 |
:setting_order => 6 |
|
| 54 |
}, |
|
| 55 |
:username => {
|
|
| 56 |
:string => '#{login}',
|
|
| 57 |
:order => %w(login id), |
|
| 58 |
:setting_order => 7 |
|
| 59 |
}, |
|
| 60 |
} |
|
| 61 |
|
|
| 62 |
MAIL_NOTIFICATION_OPTIONS = [ |
|
| 63 |
['all', :label_user_mail_option_all], |
|
| 64 |
['selected', :label_user_mail_option_selected], |
|
| 65 |
['only_my_events', :label_user_mail_option_only_my_events], |
|
| 66 |
['only_assigned', :label_user_mail_option_only_assigned], |
|
| 67 |
['only_owner', :label_user_mail_option_only_owner], |
|
| 68 |
['none', :label_user_mail_option_none] |
|
| 69 |
] |
|
| 70 |
|
|
| 71 |
has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)},
|
|
| 72 |
:after_remove => Proc.new {|user, group| group.user_removed(user)}
|
|
| 73 |
has_many :changesets, :dependent => :nullify |
|
| 74 |
has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' |
|
| 75 |
has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'" |
|
| 76 |
has_one :api_token, :class_name => 'Token', :conditions => "action='api'" |
|
| 77 |
belongs_to :auth_source |
|
| 78 |
|
|
| 79 |
scope :logged, lambda { where("#{User.table_name}.status <> #{STATUS_ANONYMOUS}") }
|
|
| 80 |
scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
|
|
| 81 |
|
|
| 82 |
acts_as_customizable |
|
| 83 |
|
|
| 84 |
attr_accessor :password, :password_confirmation |
|
| 85 |
attr_accessor :last_before_login_on |
|
| 86 |
# Prevents unauthorized assignments |
|
| 87 |
attr_protected :login, :admin, :password, :password_confirmation, :hashed_password |
|
| 88 |
|
|
| 89 |
LOGIN_LENGTH_LIMIT = 60 |
|
| 90 |
MAIL_LENGTH_LIMIT = 60 |
|
| 91 |
|
|
| 92 |
validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) }
|
|
| 93 |
validates_uniqueness_of :login, :if => Proc.new { |user| user.login_changed? && user.login.present? }, :case_sensitive => false
|
|
| 94 |
validates_uniqueness_of :mail, :if => Proc.new { |user| user.mail_changed? && user.mail.present? }, :case_sensitive => false
|
|
| 95 |
# Login must contain letters, numbers, underscores only |
|
| 96 |
validates_format_of :login, :with => /\A[a-z0-9_\-@\.]*\z/i |
|
| 97 |
validates_length_of :login, :maximum => LOGIN_LENGTH_LIMIT |
|
| 98 |
validates_length_of :firstname, :lastname, :maximum => 30 |
|
| 99 |
validates_format_of :mail, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :allow_blank => true
|
|
| 100 |
validates_length_of :mail, :maximum => MAIL_LENGTH_LIMIT, :allow_nil => true |
|
| 101 |
validates_confirmation_of :password, :allow_nil => true |
|
| 102 |
validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true |
|
| 103 |
validate :validate_password_length |
|
| 104 |
|
|
| 105 |
before_create :set_mail_notification |
|
| 106 |
before_save :update_hashed_password |
|
| 107 |
before_destroy :remove_references_before_destroy |
|
| 108 |
|
|
| 109 |
scope :in_group, lambda {|group|
|
|
| 110 |
group_id = group.is_a?(Group) ? group.id : group.to_i |
|
| 111 |
where("#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id)
|
|
| 112 |
} |
|
| 113 |
scope :not_in_group, lambda {|group|
|
|
| 114 |
group_id = group.is_a?(Group) ? group.id : group.to_i |
|
| 115 |
where("#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id)
|
|
| 116 |
} |
|
| 117 |
scope :sorted, lambda { order(*User.fields_for_order_statement)}
|
|
| 118 |
|
|
| 119 |
def set_mail_notification |
|
| 120 |
self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? |
|
| 121 |
true |
|
| 122 |
end |
|
| 123 |
|
|
| 124 |
def update_hashed_password |
|
| 125 |
# update hashed_password if password was set |
|
| 126 |
if self.password && self.auth_source_id.blank? |
|
| 127 |
salt_password(password) |
|
| 128 |
end |
|
| 129 |
end |
|
| 130 |
|
|
| 131 |
alias :base_reload :reload |
|
| 132 |
def reload(*args) |
|
| 133 |
@name = nil |
|
| 134 |
@projects_by_role = nil |
|
| 135 |
@membership_by_project_id = nil |
|
| 136 |
base_reload(*args) |
|
| 137 |
end |
|
| 138 |
|
|
| 139 |
def mail=(arg) |
|
| 140 |
write_attribute(:mail, arg.to_s.strip) |
|
| 141 |
end |
|
| 142 |
|
|
| 143 |
def identity_url=(url) |
|
| 144 |
if url.blank? |
|
| 145 |
write_attribute(:identity_url, '') |
|
| 146 |
else |
|
| 147 |
begin |
|
| 148 |
write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url)) |
|
| 149 |
rescue OpenIdAuthentication::InvalidOpenId |
|
| 150 |
# Invalid url, don't save |
|
| 151 |
end |
|
| 152 |
end |
|
| 153 |
self.read_attribute(:identity_url) |
|
| 154 |
end |
|
| 155 |
|
|
| 156 |
# Returns the user that matches provided login and password, or nil |
|
| 157 |
def self.try_to_login(login, password) |
|
| 158 |
login = login.to_s |
|
| 159 |
password = password.to_s |
|
| 160 |
|
|
| 161 |
# Make sure no one can sign in with an empty login or password |
|
| 162 |
return nil if login.empty? || password.empty? |
|
| 163 |
user = find_by_login(login) |
|
| 164 |
if user |
|
| 165 |
# user is already in local database |
|
| 166 |
return nil unless user.active? |
|
| 167 |
return nil unless user.check_password?(password) |
|
| 168 |
else |
|
| 169 |
# user is not yet registered, try to authenticate with available sources |
|
| 170 |
attrs = AuthSource.authenticate(login, password) |
|
| 171 |
if attrs |
|
| 172 |
user = new(attrs) |
|
| 173 |
user.login = login |
|
| 174 |
user.language = Setting.default_language |
|
| 175 |
if user.save |
|
| 176 |
user.reload |
|
| 177 |
logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source
|
|
| 178 |
end |
|
| 179 |
end |
|
| 180 |
end |
|
| 181 |
user.update_column(:last_login_on, Time.now) if user && !user.new_record? |
|
| 182 |
user |
|
| 183 |
rescue => text |
|
| 184 |
raise text |
|
| 185 |
end |
|
| 186 |
|
|
| 187 |
# Returns the user who matches the given autologin +key+ or nil |
|
| 188 |
def self.try_to_autologin(key) |
|
| 189 |
user = Token.find_active_user('autologin', key, Setting.autologin.to_i)
|
|
| 190 |
if user |
|
| 191 |
user.update_column(:last_login_on, Time.now) |
|
| 192 |
user |
|
| 193 |
end |
|
| 194 |
end |
|
| 195 |
|
|
| 196 |
def self.name_formatter(formatter = nil) |
|
| 197 |
USER_FORMATS[formatter || Setting.user_format] || USER_FORMATS[:firstname_lastname] |
|
| 198 |
end |
|
| 199 |
|
|
| 200 |
# Returns an array of fields names than can be used to make an order statement for users |
|
| 201 |
# according to how user names are displayed |
|
| 202 |
# Examples: |
|
| 203 |
# |
|
| 204 |
# User.fields_for_order_statement => ['users.login', 'users.id'] |
|
| 205 |
# User.fields_for_order_statement('authors') => ['authors.login', 'authors.id']
|
|
| 206 |
def self.fields_for_order_statement(table=nil) |
|
| 207 |
table ||= table_name |
|
| 208 |
name_formatter[:order].map {|field| "#{table}.#{field}"}
|
|
| 209 |
end |
|
| 210 |
|
|
| 211 |
# Return user's full name for display |
|
| 212 |
def name(formatter = nil) |
|
| 213 |
f = self.class.name_formatter(formatter) |
|
| 214 |
if formatter |
|
| 215 |
eval('"' + f[:string] + '"')
|
|
| 216 |
else |
|
| 217 |
@name ||= eval('"' + f[:string] + '"')
|
|
| 218 |
end |
|
| 219 |
end |
|
| 220 |
|
|
| 221 |
def active? |
|
| 222 |
self.status == STATUS_ACTIVE |
|
| 223 |
end |
|
| 224 |
|
|
| 225 |
def registered? |
|
| 226 |
self.status == STATUS_REGISTERED |
|
| 227 |
end |
|
| 228 |
|
|
| 229 |
def locked? |
|
| 230 |
self.status == STATUS_LOCKED |
|
| 231 |
end |
|
| 232 |
|
|
| 233 |
def activate |
|
| 234 |
self.status = STATUS_ACTIVE |
|
| 235 |
end |
|
| 236 |
|
|
| 237 |
def register |
|
| 238 |
self.status = STATUS_REGISTERED |
|
| 239 |
end |
|
| 240 |
|
|
| 241 |
def lock |
|
| 242 |
self.status = STATUS_LOCKED |
|
| 243 |
end |
|
| 244 |
|
|
| 245 |
def activate! |
|
| 246 |
update_attribute(:status, STATUS_ACTIVE) |
|
| 247 |
end |
|
| 248 |
|
|
| 249 |
def register! |
|
| 250 |
update_attribute(:status, STATUS_REGISTERED) |
|
| 251 |
end |
|
| 252 |
|
|
| 253 |
def lock! |
|
| 254 |
update_attribute(:status, STATUS_LOCKED) |
|
| 255 |
end |
|
| 256 |
|
|
| 257 |
# Returns true if +clear_password+ is the correct user's password, otherwise false |
|
| 258 |
def check_password?(clear_password) |
|
| 259 |
if auth_source_id.present? |
|
| 260 |
auth_source.authenticate(self.login, clear_password) |
|
| 261 |
else |
|
| 262 |
User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password
|
|
| 263 |
end |
|
| 264 |
end |
|
| 265 |
|
|
| 266 |
# Generates a random salt and computes hashed_password for +clear_password+ |
|
| 267 |
# The hashed password is stored in the following form: SHA1(salt + SHA1(password)) |
|
| 268 |
def salt_password(clear_password) |
|
| 269 |
self.salt = User.generate_salt |
|
| 270 |
self.hashed_password = User.hash_password("#{salt}#{User.hash_password clear_password}")
|
|
| 271 |
end |
|
| 272 |
|
|
| 273 |
# Does the backend storage allow this user to change their password? |
|
| 274 |
def change_password_allowed? |
|
| 275 |
return true if auth_source.nil? |
|
| 276 |
return auth_source.allow_password_changes? |
|
| 277 |
end |
|
| 278 |
|
|
| 279 |
# Generate and set a random password. Useful for automated user creation |
|
| 280 |
# Based on Token#generate_token_value |
|
| 281 |
# |
|
| 282 |
def random_password |
|
| 283 |
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
|
|
| 284 |
password = '' |
|
| 285 |
40.times { |i| password << chars[rand(chars.size-1)] }
|
|
| 286 |
self.password = password |
|
| 287 |
self.password_confirmation = password |
|
| 288 |
self |
|
| 289 |
end |
|
| 290 |
|
|
| 291 |
def pref |
|
| 292 |
self.preference ||= UserPreference.new(:user => self) |
|
| 293 |
end |
|
| 294 |
|
|
| 295 |
def time_zone |
|
| 296 |
@time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone]) |
|
| 297 |
end |
|
| 298 |
|
|
| 299 |
def wants_comments_in_reverse_order? |
|
| 300 |
self.pref[:comments_sorting] == 'desc' |
|
| 301 |
end |
|
| 302 |
|
|
| 303 |
# Return user's RSS key (a 40 chars long string), used to access feeds |
|
| 304 |
def rss_key |
|
| 305 |
if rss_token.nil? |
|
| 306 |
create_rss_token(:action => 'feeds') |
|
| 307 |
end |
|
| 308 |
rss_token.value |
|
| 309 |
end |
|
| 310 |
|
|
| 311 |
# Return user's API key (a 40 chars long string), used to access the API |
|
| 312 |
def api_key |
|
| 313 |
if api_token.nil? |
|
| 314 |
create_api_token(:action => 'api') |
|
| 315 |
end |
|
| 316 |
api_token.value |
|
| 317 |
end |
|
| 318 |
|
|
| 319 |
# Return an array of project ids for which the user has explicitly turned mail notifications on |
|
| 320 |
def notified_projects_ids |
|
| 321 |
@notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id)
|
|
| 322 |
end |
|
| 323 |
|
|
| 324 |
def notified_project_ids=(ids) |
|
| 325 |
Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id])
|
|
| 326 |
Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty?
|
|
| 327 |
@notified_projects_ids = nil |
|
| 328 |
notified_projects_ids |
|
| 329 |
end |
|
| 330 |
|
|
| 331 |
def valid_notification_options |
|
| 332 |
self.class.valid_notification_options(self) |
|
| 333 |
end |
|
| 334 |
|
|
| 335 |
# Only users that belong to more than 1 project can select projects for which they are notified |
|
| 336 |
def self.valid_notification_options(user=nil) |
|
| 337 |
# Note that @user.membership.size would fail since AR ignores |
|
| 338 |
# :include association option when doing a count |
|
| 339 |
if user.nil? || user.memberships.length < 1 |
|
| 340 |
MAIL_NOTIFICATION_OPTIONS.reject {|option| option.first == 'selected'}
|
|
| 341 |
else |
|
| 342 |
MAIL_NOTIFICATION_OPTIONS |
|
| 343 |
end |
|
| 344 |
end |
|
| 345 |
|
|
| 346 |
# Find a user account by matching the exact login and then a case-insensitive |
|
| 347 |
# version. Exact matches will be given priority. |
|
| 348 |
def self.find_by_login(login) |
|
| 349 |
if login.present? |
|
| 350 |
login = login.to_s |
|
| 351 |
# First look for an exact match |
|
| 352 |
user = where(:login => login).all.detect {|u| u.login == login}
|
|
| 353 |
unless user |
|
| 354 |
# Fail over to case-insensitive if none was found |
|
| 355 |
user = where("LOWER(login) = ?", login.downcase).first
|
|
| 356 |
end |
|
| 357 |
user |
|
| 358 |
end |
|
| 359 |
end |
|
| 360 |
|
|
| 361 |
def self.find_by_rss_key(key) |
|
| 362 |
Token.find_active_user('feeds', key)
|
|
| 363 |
end |
|
| 364 |
|
|
| 365 |
def self.find_by_api_key(key) |
|
| 366 |
Token.find_active_user('api', key)
|
|
| 367 |
end |
|
| 368 |
|
|
| 369 |
# Makes find_by_mail case-insensitive |
|
| 370 |
def self.find_by_mail(mail) |
|
| 371 |
where("LOWER(mail) = ?", mail.to_s.downcase).first
|
|
| 372 |
end |
|
| 373 |
|
|
| 374 |
# Returns true if the default admin account can no longer be used |
|
| 375 |
def self.default_admin_account_changed? |
|
| 376 |
!User.active.find_by_login("admin").try(:check_password?, "admin")
|
|
| 377 |
end |
|
| 378 |
|
|
| 379 |
def to_s |
|
| 380 |
name |
|
| 381 |
end |
|
| 382 |
|
|
| 383 |
CSS_CLASS_BY_STATUS = {
|
|
| 384 |
STATUS_ANONYMOUS => 'anon', |
|
| 385 |
STATUS_ACTIVE => 'active', |
|
| 386 |
STATUS_REGISTERED => 'registered', |
|
| 387 |
STATUS_LOCKED => 'locked' |
|
| 388 |
} |
|
| 389 |
|
|
| 390 |
def css_classes |
|
| 391 |
"user #{CSS_CLASS_BY_STATUS[status]}"
|
|
| 392 |
end |
|
| 393 |
|
|
| 394 |
# Returns the current day according to user's time zone |
|
| 395 |
def today |
|
| 396 |
if time_zone.nil? |
|
| 397 |
Date.today |
|
| 398 |
else |
|
| 399 |
Time.now.in_time_zone(time_zone).to_date |
|
| 400 |
end |
|
| 401 |
end |
|
| 402 |
|
|
| 403 |
# Returns the day of +time+ according to user's time zone |
|
| 404 |
def time_to_date(time) |
|
| 405 |
if time_zone.nil? |
|
| 406 |
time.to_date |
|
| 407 |
else |
|
| 408 |
time.in_time_zone(time_zone).to_date |
|
| 409 |
end |
|
| 410 |
end |
|
| 411 |
|
|
| 412 |
def logged? |
|
| 413 |
true |
|
| 414 |
end |
|
| 415 |
|
|
| 416 |
def anonymous? |
|
| 417 |
!logged? |
|
| 418 |
end |
|
| 419 |
|
|
| 420 |
# Returns user's membership for the given project |
|
| 421 |
# or nil if the user is not a member of project |
|
| 422 |
def membership(project) |
|
| 423 |
project_id = project.is_a?(Project) ? project.id : project |
|
| 424 |
|
|
| 425 |
@membership_by_project_id ||= Hash.new {|h, project_id|
|
|
| 426 |
h[project_id] = memberships.where(:project_id => project_id).first |
|
| 427 |
} |
|
| 428 |
@membership_by_project_id[project_id] |
|
| 429 |
end |
|
| 430 |
|
|
| 431 |
# Return user's roles for project |
|
| 432 |
def roles_for_project(project) |
|
| 433 |
roles = [] |
|
| 434 |
# No role on archived projects |
|
| 435 |
return roles if project.nil? || project.archived? |
|
| 436 |
if logged? |
|
| 437 |
# Find project membership |
|
| 438 |
membership = membership(project) |
|
| 439 |
if membership |
|
| 440 |
roles = membership.roles |
|
| 441 |
else |
|
| 442 |
@role_non_member ||= Role.non_member |
|
| 443 |
roles << @role_non_member |
|
| 444 |
end |
|
| 445 |
else |
|
| 446 |
@role_anonymous ||= Role.anonymous |
|
| 447 |
roles << @role_anonymous |
|
| 448 |
end |
|
| 449 |
roles |
|
| 450 |
end |
|
| 451 |
|
|
| 452 |
# Return true if the user is a member of project |
|
| 453 |
def member_of?(project) |
|
| 454 |
projects.to_a.include?(project) |
|
| 455 |
end |
|
| 456 |
|
|
| 457 |
# Returns a hash of user's projects grouped by roles |
|
| 458 |
def projects_by_role |
|
| 459 |
return @projects_by_role if @projects_by_role |
|
| 460 |
|
|
| 461 |
@projects_by_role = Hash.new([]) |
|
| 462 |
memberships.each do |membership| |
|
| 463 |
if membership.project |
|
| 464 |
membership.roles.each do |role| |
|
| 465 |
@projects_by_role[role] = [] unless @projects_by_role.key?(role) |
|
| 466 |
@projects_by_role[role] << membership.project |
|
| 467 |
end |
|
| 468 |
end |
|
| 469 |
end |
|
| 470 |
@projects_by_role.each do |role, projects| |
|
| 471 |
projects.uniq! |
|
| 472 |
end |
|
| 473 |
|
|
| 474 |
@projects_by_role |
|
| 475 |
end |
|
| 476 |
|
|
| 477 |
# Returns true if user is arg or belongs to arg |
|
| 478 |
def is_or_belongs_to?(arg) |
|
| 479 |
if arg.is_a?(User) |
|
| 480 |
self == arg |
|
| 481 |
elsif arg.is_a?(Group) |
|
| 482 |
arg.users.include?(self) |
|
| 483 |
else |
|
| 484 |
false |
|
| 485 |
end |
|
| 486 |
end |
|
| 487 |
|
|
| 488 |
# Return true if the user is allowed to do the specified action on a specific context |
|
| 489 |
# Action can be: |
|
| 490 |
# * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') |
|
| 491 |
# * a permission Symbol (eg. :edit_project) |
|
| 492 |
# Context can be: |
|
| 493 |
# * a project : returns true if user is allowed to do the specified action on this project |
|
| 494 |
# * an array of projects : returns true if user is allowed on every project |
|
| 495 |
# * nil with options[:global] set : check if user has at least one role allowed for this action, |
|
| 496 |
# or falls back to Non Member / Anonymous permissions depending if the user is logged |
|
| 497 |
def allowed_to?(action, context, options={}, &block)
|
|
| 498 |
if context && context.is_a?(Project) |
|
| 499 |
return false unless context.allows_to?(action) |
|
| 500 |
# Admin users are authorized for anything else |
|
| 501 |
return true if admin? |
|
| 502 |
|
|
| 503 |
roles = roles_for_project(context) |
|
| 504 |
return false unless roles |
|
| 505 |
roles.any? {|role|
|
|
| 506 |
(context.is_public? || role.member?) && |
|
| 507 |
role.allowed_to?(action) && |
|
| 508 |
(block_given? ? yield(role, self) : true) |
|
| 509 |
} |
|
| 510 |
elsif context && context.is_a?(Array) |
|
| 511 |
if context.empty? |
|
| 512 |
false |
|
| 513 |
else |
|
| 514 |
# Authorize if user is authorized on every element of the array |
|
| 515 |
context.map {|project| allowed_to?(action, project, options, &block)}.reduce(:&)
|
|
| 516 |
end |
|
| 517 |
elsif options[:global] |
|
| 518 |
# Admin users are always authorized |
|
| 519 |
return true if admin? |
|
| 520 |
|
|
| 521 |
# authorize if user has at least one role that has this permission |
|
| 522 |
roles = memberships.collect {|m| m.roles}.flatten.uniq
|
|
| 523 |
roles << (self.logged? ? Role.non_member : Role.anonymous) |
|
| 524 |
roles.any? {|role|
|
|
| 525 |
role.allowed_to?(action) && |
|
| 526 |
(block_given? ? yield(role, self) : true) |
|
| 527 |
} |
|
| 528 |
else |
|
| 529 |
false |
|
| 530 |
end |
|
| 531 |
end |
|
| 532 |
|
|
| 533 |
# Is the user allowed to do the specified action on any project? |
|
| 534 |
# See allowed_to? for the actions and valid options. |
|
| 535 |
def allowed_to_globally?(action, options, &block) |
|
| 536 |
allowed_to?(action, nil, options.reverse_merge(:global => true), &block) |
|
| 537 |
end |
|
| 538 |
|
|
| 539 |
# Returns true if the user is allowed to delete his own account |
|
| 540 |
def own_account_deletable? |
|
| 541 |
Setting.unsubscribe? && |
|
| 542 |
(!admin? || User.active.where("admin = ? AND id <> ?", true, id).exists?)
|
|
| 543 |
end |
|
| 544 |
|
|
| 545 |
safe_attributes 'login', |
|
| 546 |
'firstname', |
|
| 547 |
'lastname', |
|
| 548 |
'mail', |
|
| 549 |
'mail_notification', |
|
| 550 |
'language', |
|
| 551 |
'custom_field_values', |
|
| 552 |
'custom_fields', |
|
| 553 |
'identity_url' |
|
| 554 |
|
|
| 555 |
safe_attributes 'status', |
|
| 556 |
'auth_source_id', |
|
| 557 |
:if => lambda {|user, current_user| current_user.admin?}
|
|
| 558 |
|
|
| 559 |
safe_attributes 'group_ids', |
|
| 560 |
:if => lambda {|user, current_user| current_user.admin? && !user.new_record?}
|
|
| 561 |
|
|
| 562 |
# Utility method to help check if a user should be notified about an |
|
| 563 |
# event. |
|
| 564 |
# |
|
| 565 |
# TODO: only supports Issue events currently |
|
| 566 |
def notify_about?(object) |
|
| 567 |
if mail_notification == 'all' |
|
| 568 |
true |
|
| 569 |
elsif mail_notification.blank? || mail_notification == 'none' |
|
| 570 |
false |
|
| 571 |
else |
|
| 572 |
case object |
|
| 573 |
when Issue |
|
| 574 |
case mail_notification |
|
| 575 |
when 'selected', 'only_my_events' |
|
| 576 |
# user receives notifications for created/assigned issues on unselected projects |
|
| 577 |
object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was) |
|
| 578 |
when 'only_assigned' |
|
| 579 |
is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was) |
|
| 580 |
when 'only_owner' |
|
| 581 |
object.author == self |
|
| 582 |
end |
|
| 583 |
when News |
|
| 584 |
# always send to project members except when mail_notification is set to 'none' |
|
| 585 |
true |
|
| 586 |
end |
|
| 587 |
end |
|
| 588 |
end |
|
| 589 |
|
|
| 590 |
def self.current=(user) |
|
| 591 |
Thread.current[:current_user] = user |
|
| 592 |
end |
|
| 593 |
|
|
| 594 |
def self.current |
|
| 595 |
Thread.current[:current_user] ||= User.anonymous |
|
| 596 |
end |
|
| 597 |
|
|
| 598 |
# Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only |
|
| 599 |
# one anonymous user per database. |
|
| 600 |
def self.anonymous |
|
| 601 |
anonymous_user = AnonymousUser.first |
|
| 602 |
if anonymous_user.nil? |
|
| 603 |
anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0) |
|
| 604 |
raise 'Unable to create the anonymous user.' if anonymous_user.new_record? |
|
| 605 |
end |
|
| 606 |
anonymous_user |
|
| 607 |
end |
|
| 608 |
|
|
| 609 |
# Salts all existing unsalted passwords |
|
| 610 |
# It changes password storage scheme from SHA1(password) to SHA1(salt + SHA1(password)) |
|
| 611 |
# This method is used in the SaltPasswords migration and is to be kept as is |
|
| 612 |
def self.salt_unsalted_passwords! |
|
| 613 |
transaction do |
|
| 614 |
User.where("salt IS NULL OR salt = ''").find_each do |user|
|
|
| 615 |
next if user.hashed_password.blank? |
|
| 616 |
salt = User.generate_salt |
|
| 617 |
hashed_password = User.hash_password("#{salt}#{user.hashed_password}")
|
|
| 618 |
User.where(:id => user.id).update_all(:salt => salt, :hashed_password => hashed_password) |
|
| 619 |
end |
|
| 620 |
end |
|
| 621 |
end |
|
| 622 |
|
|
| 623 |
protected |
|
| 624 |
|
|
| 625 |
def validate_password_length |
|
| 626 |
# Password length validation based on setting |
|
| 627 |
if !password.nil? && password.size < Setting.password_min_length.to_i |
|
| 628 |
errors.add(:password, :too_short, :count => Setting.password_min_length.to_i) |
|
| 629 |
end |
|
| 630 |
end |
|
| 631 |
|
|
| 632 |
private |
|
| 633 |
|
|
| 634 |
# Removes references that are not handled by associations |
|
| 635 |
# Things that are not deleted are reassociated with the anonymous user |
|
| 636 |
def remove_references_before_destroy |
|
| 637 |
return if self.id.nil? |
|
| 638 |
|
|
| 639 |
substitute = User.anonymous |
|
| 640 |
Attachment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
|
| 641 |
Comment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
|
| 642 |
Issue.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
|
| 643 |
Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id] |
|
| 644 |
Journal.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] |
|
| 645 |
JournalDetail.update_all ['old_value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s] |
|
| 646 |
JournalDetail.update_all ['value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND value = ?", id.to_s] |
|
| 647 |
Message.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
|
| 648 |
News.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
|
| 649 |
# Remove private queries and keep public ones |
|
| 650 |
::Query.delete_all ['user_id = ? AND is_public = ?', id, false] |
|
| 651 |
::Query.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] |
|
| 652 |
TimeEntry.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] |
|
| 653 |
Token.delete_all ['user_id = ?', id] |
|
| 654 |
Watcher.delete_all ['user_id = ?', id] |
|
| 655 |
WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
|
| 656 |
WikiContent::Version.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
|
| 657 |
end |
|
| 658 |
|
|
| 659 |
# Return password digest |
|
| 660 |
def self.hash_password(clear_password) |
|
| 661 |
Digest::SHA1.hexdigest(clear_password || "") |
|
| 662 |
end |
|
| 663 |
|
|
| 664 |
# Returns a 128bits random salt as a hex string (32 chars long) |
|
| 665 |
def self.generate_salt |
|
| 666 |
Redmine::Utils.random_hex(16) |
|
| 667 |
end |
|
| 668 |
|
|
| 669 |
end |
|
| 670 |
|
|
| 671 |
class AnonymousUser < User |
|
| 672 |
validate :validate_anonymous_uniqueness, :on => :create |
|
| 673 |
|
|
| 674 |
def validate_anonymous_uniqueness |
|
| 675 |
# There should be only one AnonymousUser in the database |
|
| 676 |
errors.add :base, 'An anonymous user already exists.' if AnonymousUser.exists? |
|
| 677 |
end |
|
| 678 |
|
|
| 679 |
def available_custom_fields |
|
| 680 |
[] |
|
| 681 |
end |
|
| 682 |
|
|
| 683 |
# Overrides a few properties |
|
| 684 |
def logged?; false end |
|
| 685 |
def admin; false end |
|
| 686 |
def name(*args); I18n.t(:label_user_anonymous) end |
|
| 687 |
def mail; nil end |
|
| 688 |
def time_zone; nil end |
|
| 689 |
def rss_key; nil end |
|
| 690 |
|
|
| 691 |
def pref |
|
| 692 |
UserPreference.new(:user => self) |
|
| 693 |
end |
|
| 694 |
|
|
| 695 |
def member_of?(project) |
|
| 696 |
false |
|
| 697 |
end |
|
| 698 |
|
|
| 699 |
# Anonymous user can not be destroyed |
|
| 700 |
def destroy |
|
| 701 |
false |
|
| 702 |
end |
|
| 703 |
end |
|
| .svn/pristine/06/066f939eced969c1eec65f1fe6ed629130668c67.svn-base | ||
|---|---|---|
| 1 |
# encoding: utf-8 |
|
| 2 |
# |
|
| 3 |
# Redmine - project management software |
|
| 4 |
# Copyright (C) 2006-2011 Jean-Philippe Lang |
|
| 5 |
# |
|
| 6 |
# This program is free software; you can redistribute it and/or |
|
| 7 |
# modify it under the terms of the GNU General Public License |
|
| 8 |
# as published by the Free Software Foundation; either version 2 |
|
| 9 |
# of the License, or (at your option) any later version. |
|
| 10 |
# |
|
| 11 |
# This program is distributed in the hope that it will be useful, |
|
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 14 |
# GNU General Public License for more details. |
|
| 15 |
# |
|
| 16 |
# You should have received a copy of the GNU General Public License |
|
| 17 |
# along with this program; if not, write to the Free Software |
|
| 18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
| 19 |
|
|
| 20 |
module IssueRelationsHelper |
|
| 21 |
def collection_for_relation_type_select |
|
| 22 |
values = IssueRelation::TYPES |
|
| 23 |
values.keys.sort{|x,y| values[x][:order] <=> values[y][:order]}.collect{|k| [l(values[k][:name]), k]}
|
|
| 24 |
end |
|
| 25 |
end |
|
| .svn/pristine/06/067fc70eea1018fdccc706144d28aa0ea0565e34.svn-base | ||
|---|---|---|
| 1 |
--- |
|
| 2 |
watchers_001: |
|
| 3 |
watchable_type: Issue |
|
| 4 |
watchable_id: 2 |
|
| 5 |
user_id: 3 |
|
| 6 |
watchers_002: |
|
| 7 |
watchable_type: Message |
|
| 8 |
watchable_id: 1 |
|
| 9 |
user_id: 1 |
|
| 10 |
watchers_003: |
|
| 11 |
watchable_type: Issue |
|
| 12 |
watchable_id: 2 |
|
| 13 |
user_id: 1 |
|
| 14 |
|
|
| .svn/pristine/06/068885f0c95ca2ed440e38b13dd95f7bb13f11dc.svn-base | ||
|---|---|---|
| 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 |
require File.expand_path('../../test_helper', __FILE__)
|
|
| 19 |
|
|
| 20 |
class CustomFieldTest < ActiveSupport::TestCase |
|
| 21 |
fixtures :custom_fields |
|
| 22 |
|
|
| 23 |
def test_create |
|
| 24 |
field = UserCustomField.new(:name => 'Money money money', :field_format => 'float') |
|
| 25 |
assert field.save |
|
| 26 |
end |
|
| 27 |
|
|
| 28 |
def test_regexp_validation |
|
| 29 |
field = IssueCustomField.new(:name => 'regexp', :field_format => 'text', :regexp => '[a-z0-9') |
|
| 30 |
assert !field.save |
|
| 31 |
assert_equal I18n.t('activerecord.errors.messages.invalid'), field.errors.on(:regexp)
|
|
| 32 |
|
|
| 33 |
field.regexp = '[a-z0-9]' |
|
| 34 |
assert field.save |
|
| 35 |
end |
|
| 36 |
|
|
| 37 |
def test_possible_values_should_accept_an_array |
|
| 38 |
field = CustomField.new |
|
| 39 |
field.possible_values = ["One value", ""] |
|
| 40 |
assert_equal ["One value"], field.possible_values |
|
| 41 |
end |
|
| 42 |
|
|
| 43 |
def test_possible_values_should_accept_a_string |
|
| 44 |
field = CustomField.new |
|
| 45 |
field.possible_values = "One value" |
|
| 46 |
assert_equal ["One value"], field.possible_values |
|
| 47 |
end |
|
| 48 |
|
|
| 49 |
def test_possible_values_should_accept_a_multiline_string |
|
| 50 |
field = CustomField.new |
|
| 51 |
field.possible_values = "One value\nAnd another one \r\n \n" |
|
| 52 |
assert_equal ["One value", "And another one"], field.possible_values |
|
| 53 |
end |
|
| 54 |
|
|
| 55 |
def test_destroy |
|
| 56 |
field = CustomField.find(1) |
|
| 57 |
assert field.destroy |
|
| 58 |
end |
|
| 59 |
end |
|
| .svn/pristine/06/06aa9d1d0636535d93ef529aee72095eced64467.svn-base | ||
|---|---|---|
| 1 |
<%= call_hook(:view_repositories_show_contextual, { :repository => @repository, :project => @project }) %>
|
|
| 2 |
|
|
| 3 |
<div class="contextual"> |
|
| 4 |
<%= render :partial => 'navigation' %> |
|
| 5 |
</div> |
|
| 6 |
|
|
| 7 |
<h2> |
|
| 8 |
<%= render :partial => 'breadcrumbs', :locals => { :path => @path, :kind => (@entry ? @entry.kind : nil), :revision => @rev } %>
|
|
| 9 |
</h2> |
|
| 10 |
|
|
| 11 |
<p><%= render :partial => 'link_to_functions' %></p> |
|
| 12 |
|
|
| 13 |
<%= render_properties(@properties) %> |
|
| 14 |
|
|
| 15 |
<%= render(:partial => 'revisions', |
|
| 16 |
:locals => {:project => @project, :path => @path, :revisions => @changesets, :entry => @entry }) unless @changesets.empty? %>
|
|
| 17 |
|
|
| 18 |
<% html_title(l(:label_change_plural)) -%> |
|
| .svn/pristine/06/06b89fac956f5eaa8ae34a2daf2088c102b5cfb1.svn-base | ||
|---|---|---|
| 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 Enumeration < ActiveRecord::Base |
|
| 19 |
default_scope :order => "#{Enumeration.table_name}.position ASC"
|
|
| 20 |
|
|
| 21 |
belongs_to :project |
|
| 22 |
|
|
| 23 |
acts_as_list :scope => 'type = \'#{type}\''
|
|
| 24 |
acts_as_customizable |
|
| 25 |
acts_as_tree :order => 'position ASC' |
|
| 26 |
|
|
| 27 |
before_destroy :check_integrity |
|
| 28 |
before_save :check_default |
|
| 29 |
|
|
| 30 |
validates_presence_of :name |
|
| 31 |
validates_uniqueness_of :name, :scope => [:type, :project_id] |
|
| 32 |
validates_length_of :name, :maximum => 30 |
|
| 33 |
|
|
| 34 |
named_scope :shared, :conditions => { :project_id => nil }
|
|
| 35 |
named_scope :active, :conditions => { :active => true }
|
|
| 36 |
named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
|
|
| 37 |
|
|
| 38 |
def self.default |
|
| 39 |
# Creates a fake default scope so Enumeration.default will check |
|
| 40 |
# it's type. STI subclasses will automatically add their own |
|
| 41 |
# types to the finder. |
|
| 42 |
if self.descends_from_active_record? |
|
| 43 |
find(:first, :conditions => { :is_default => true, :type => 'Enumeration' })
|
|
| 44 |
else |
|
| 45 |
# STI classes are |
|
| 46 |
find(:first, :conditions => { :is_default => true })
|
|
| 47 |
end |
|
| 48 |
end |
|
| 49 |
|
|
| 50 |
# Overloaded on concrete classes |
|
| 51 |
def option_name |
|
| 52 |
nil |
|
| 53 |
end |
|
| 54 |
|
|
| 55 |
def check_default |
|
| 56 |
if is_default? && is_default_changed? |
|
| 57 |
Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type})
|
|
| 58 |
end |
|
| 59 |
end |
|
| 60 |
|
|
| 61 |
# Overloaded on concrete classes |
|
| 62 |
def objects_count |
|
| 63 |
0 |
|
| 64 |
end |
|
| 65 |
|
|
| 66 |
def in_use? |
|
| 67 |
self.objects_count != 0 |
|
| 68 |
end |
|
| 69 |
|
|
| 70 |
# Is this enumeration overiding a system level enumeration? |
|
| 71 |
def is_override? |
|
| 72 |
!self.parent.nil? |
|
| 73 |
end |
|
| 74 |
|
|
| 75 |
alias :destroy_without_reassign :destroy |
|
| 76 |
|
|
| 77 |
# Destroy the enumeration |
|
| 78 |
# If a enumeration is specified, objects are reassigned |
|
| 79 |
def destroy(reassign_to = nil) |
|
| 80 |
if reassign_to && reassign_to.is_a?(Enumeration) |
|
| 81 |
self.transfer_relations(reassign_to) |
|
| 82 |
end |
|
| 83 |
destroy_without_reassign |
|
| 84 |
end |
|
| 85 |
|
|
| 86 |
def <=>(enumeration) |
|
| 87 |
position <=> enumeration.position |
|
| 88 |
end |
|
| 89 |
|
|
| 90 |
def to_s; name end |
|
| 91 |
|
|
| 92 |
# Returns the Subclasses of Enumeration. Each Subclass needs to be |
|
| 93 |
# required in development mode. |
|
| 94 |
# |
|
| 95 |
# Note: subclasses is protected in ActiveRecord |
|
| 96 |
def self.get_subclasses |
|
| 97 |
@@subclasses[Enumeration] |
|
| 98 |
end |
|
| 99 |
|
|
| 100 |
# Does the +new+ Hash override the previous Enumeration? |
|
| 101 |
def self.overridding_change?(new, previous) |
|
| 102 |
if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous) |
|
| 103 |
return false |
|
| 104 |
else |
|
| 105 |
return true |
|
| 106 |
end |
|
| 107 |
end |
|
| 108 |
|
|
| 109 |
# Does the +new+ Hash have the same custom values as the previous Enumeration? |
|
| 110 |
def self.same_custom_values?(new, previous) |
|
| 111 |
previous.custom_field_values.each do |custom_value| |
|
| 112 |
if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s] |
|
| 113 |
return false |
|
| 114 |
end |
|
| 115 |
end |
|
| 116 |
|
|
| 117 |
return true |
|
| 118 |
end |
|
| 119 |
|
|
| 120 |
# Are the new and previous fields equal? |
|
| 121 |
def self.same_active_state?(new, previous) |
|
| 122 |
new = (new == "1" ? true : false) |
|
| 123 |
return new == previous |
|
| 124 |
end |
|
| 125 |
|
|
| 126 |
private |
|
| 127 |
def check_integrity |
|
| 128 |
raise "Can't delete enumeration" if self.in_use? |
|
| 129 |
end |
|
| 130 |
|
|
| 131 |
end |
|
| 132 |
|
|
| 133 |
# Force load the subclasses in development mode |
|
| 134 |
require_dependency 'time_entry_activity' |
|
| 135 |
require_dependency 'document_category' |
|
| 136 |
require_dependency 'issue_priority' |
|
| .svn/pristine/06/06d13f36c723de5d4d11734c74881a345af8508f.svn-base | ||
|---|---|---|
| 1 |
<%= TestHelper.view_path_for __FILE__ %> (from app) |
|
| .svn/pristine/06/06efa9c81bf4ffa47e3211023e46fcb09d4eac0f.svn-base | ||
|---|---|---|
| 1 |
# 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 |
require File.expand_path('../../test_helper', __FILE__)
|
|
| 19 |
|
|
| 20 |
class RolesControllerTest < ActionController::TestCase |
|
| 21 |
fixtures :roles, :users, :members, :member_roles, :workflows, :trackers |
|
| 22 |
|
|
| 23 |
def setup |
|
| 24 |
User.current = nil |
|
| 25 |
@request.session[:user_id] = 1 # admin |
|
| 26 |
end |
|
| 27 |
|
|
| 28 |
def test_index |
|
| 29 |
get :index |
|
| 30 |
assert_response :success |
|
| 31 |
assert_template 'index' |
|
| 32 |
|
|
| 33 |
assert_not_nil assigns(:roles) |
|
| 34 |
assert_equal Role.order('builtin, position').all, assigns(:roles)
|
|
| 35 |
|
|
| 36 |
assert_tag :tag => 'a', :attributes => { :href => '/roles/1/edit' },
|
|
| 37 |
:content => 'Manager' |
|
| 38 |
end |
|
| 39 |
|
|
| 40 |
def test_new |
|
| 41 |
get :new |
|
| 42 |
assert_response :success |
|
| 43 |
assert_template 'new' |
|
| 44 |
end |
|
| 45 |
|
|
| 46 |
def test_new_with_copy |
|
| 47 |
copy_from = Role.find(2) |
|
| 48 |
|
|
| 49 |
get :new, :copy => copy_from.id.to_s |
|
| 50 |
assert_response :success |
|
| 51 |
assert_template 'new' |
|
| 52 |
|
|
| 53 |
role = assigns(:role) |
|
| 54 |
assert_equal copy_from.permissions, role.permissions |
|
| 55 |
|
|
| 56 |
assert_select 'form' do |
|
| 57 |
# blank name |
|
| 58 |
assert_select 'input[name=?][value=]', 'role[name]' |
|
| 59 |
# edit_project permission checked |
|
| 60 |
assert_select 'input[type=checkbox][name=?][value=edit_project][checked=checked]', 'role[permissions][]' |
|
| 61 |
# add_project permission not checked |
|
| 62 |
assert_select 'input[type=checkbox][name=?][value=add_project]', 'role[permissions][]' |
|
| 63 |
assert_select 'input[type=checkbox][name=?][value=add_project][checked=checked]', 'role[permissions][]', 0 |
|
| 64 |
# workflow copy selected |
|
| 65 |
assert_select 'select[name=?]', 'copy_workflow_from' do |
|
| 66 |
assert_select 'option[value=2][selected=selected]' |
|
| 67 |
end |
|
| 68 |
end |
|
| 69 |
end |
|
| 70 |
|
|
| 71 |
def test_create_with_validaton_failure |
|
| 72 |
post :create, :role => {:name => '',
|
|
| 73 |
:permissions => ['add_issues', 'edit_issues', 'log_time', ''], |
|
| 74 |
:assignable => '0'} |
|
| 75 |
|
|
| 76 |
assert_response :success |
|
| 77 |
assert_template 'new' |
|
| 78 |
assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation' }
|
|
| 79 |
end |
|
| 80 |
|
|
| 81 |
def test_create_without_workflow_copy |
|
| 82 |
post :create, :role => {:name => 'RoleWithoutWorkflowCopy',
|
|
| 83 |
:permissions => ['add_issues', 'edit_issues', 'log_time', ''], |
|
| 84 |
:assignable => '0'} |
|
| 85 |
|
|
| 86 |
assert_redirected_to '/roles' |
|
| 87 |
role = Role.find_by_name('RoleWithoutWorkflowCopy')
|
|
| 88 |
assert_not_nil role |
|
| 89 |
assert_equal [:add_issues, :edit_issues, :log_time], role.permissions |
|
| 90 |
assert !role.assignable? |
|
| 91 |
end |
|
| 92 |
|
|
| 93 |
def test_create_with_workflow_copy |
|
| 94 |
post :create, :role => {:name => 'RoleWithWorkflowCopy',
|
|
| 95 |
:permissions => ['add_issues', 'edit_issues', 'log_time', ''], |
|
| 96 |
:assignable => '0'}, |
|
| 97 |
:copy_workflow_from => '1' |
|
| 98 |
|
|
| 99 |
assert_redirected_to '/roles' |
|
| 100 |
role = Role.find_by_name('RoleWithWorkflowCopy')
|
|
| 101 |
assert_not_nil role |
|
| 102 |
assert_equal Role.find(1).workflow_rules.size, role.workflow_rules.size |
|
| 103 |
end |
|
| 104 |
|
|
| 105 |
def test_edit |
|
| 106 |
get :edit, :id => 1 |
|
| 107 |
assert_response :success |
|
| 108 |
assert_template 'edit' |
|
| 109 |
assert_equal Role.find(1), assigns(:role) |
|
| 110 |
assert_select 'select[name=?]', 'role[issues_visibility]' |
|
| 111 |
end |
|
| 112 |
|
|
| 113 |
def test_edit_anonymous |
|
| 114 |
get :edit, :id => Role.anonymous.id |
|
| 115 |
assert_response :success |
|
| 116 |
assert_template 'edit' |
|
| 117 |
assert_select 'select[name=?]', 'role[issues_visibility]', 0 |
|
| 118 |
end |
|
| 119 |
|
|
| 120 |
def test_edit_invalid_should_respond_with_404 |
|
| 121 |
get :edit, :id => 999 |
|
| 122 |
assert_response 404 |
|
| 123 |
end |
|
| 124 |
|
|
| 125 |
def test_update |
|
| 126 |
put :update, :id => 1, |
|
| 127 |
:role => {:name => 'Manager',
|
|
| 128 |
:permissions => ['edit_project', ''], |
|
| 129 |
:assignable => '0'} |
|
| 130 |
|
|
| 131 |
assert_redirected_to '/roles' |
|
| 132 |
role = Role.find(1) |
|
| 133 |
assert_equal [:edit_project], role.permissions |
|
| 134 |
end |
|
| 135 |
|
|
| 136 |
def test_update_with_failure |
|
| 137 |
put :update, :id => 1, :role => {:name => ''}
|
|
| 138 |
assert_response :success |
|
| 139 |
assert_template 'edit' |
|
| 140 |
end |
|
| 141 |
|
|
| 142 |
def test_destroy |
|
| 143 |
r = Role.create!(:name => 'ToBeDestroyed', :permissions => [:view_wiki_pages]) |
|
| 144 |
|
|
| 145 |
delete :destroy, :id => r |
|
| 146 |
assert_redirected_to '/roles' |
|
| 147 |
assert_nil Role.find_by_id(r.id) |
|
| 148 |
end |
|
| 149 |
|
|
| 150 |
def test_destroy_role_in_use |
|
| 151 |
delete :destroy, :id => 1 |
|
| 152 |
assert_redirected_to '/roles' |
|
| 153 |
assert_equal 'This role is in use and cannot be deleted.', flash[:error] |
|
| 154 |
assert_not_nil Role.find_by_id(1) |
|
| 155 |
end |
|
| 156 |
|
|
| 157 |
def test_get_permissions |
|
| 158 |
get :permissions |
|
| 159 |
assert_response :success |
|
| 160 |
assert_template 'permissions' |
|
| 161 |
|
|
| 162 |
assert_not_nil assigns(:roles) |
|
| 163 |
assert_equal Role.order('builtin, position').all, assigns(:roles)
|
|
| 164 |
|
|
| 165 |
assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
|
|
| 166 |
:name => 'permissions[3][]', |
|
| 167 |
:value => 'add_issues', |
|
| 168 |
:checked => 'checked' } |
|
| 169 |
|
|
| 170 |
assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
|
|
| 171 |
:name => 'permissions[3][]', |
|
| 172 |
:value => 'delete_issues', |
|
| 173 |
:checked => nil } |
|
| 174 |
end |
|
| 175 |
|
|
| 176 |
def test_post_permissions |
|
| 177 |
post :permissions, :permissions => { '0' => '', '1' => ['edit_issues'], '3' => ['add_issues', 'delete_issues']}
|
|
| 178 |
assert_redirected_to '/roles' |
|
| 179 |
|
|
| 180 |
assert_equal [:edit_issues], Role.find(1).permissions |
|
| 181 |
assert_equal [:add_issues, :delete_issues], Role.find(3).permissions |
|
| 182 |
assert Role.find(2).permissions.empty? |
|
| 183 |
end |
|
| 184 |
|
|
| 185 |
def test_clear_all_permissions |
|
| 186 |
post :permissions, :permissions => { '0' => '' }
|
|
| 187 |
assert_redirected_to '/roles' |
|
| 188 |
assert Role.find(1).permissions.empty? |
|
| 189 |
end |
|
| 190 |
|
|
| 191 |
def test_move_highest |
|
| 192 |
put :update, :id => 3, :role => {:move_to => 'highest'}
|
|
| 193 |
assert_redirected_to '/roles' |
|
| 194 |
assert_equal 1, Role.find(3).position |
|
| 195 |
end |
|
| 196 |
|
|
| 197 |
def test_move_higher |
|
| 198 |
position = Role.find(3).position |
|
| 199 |
put :update, :id => 3, :role => {:move_to => 'higher'}
|
|
| 200 |
assert_redirected_to '/roles' |
|
| 201 |
assert_equal position - 1, Role.find(3).position |
|
| 202 |
end |
|
| 203 |
|
|
| 204 |
def test_move_lower |
|
| 205 |
position = Role.find(2).position |
|
| 206 |
put :update, :id => 2, :role => {:move_to => 'lower'}
|
|
| 207 |
assert_redirected_to '/roles' |
|
| 208 |
assert_equal position + 1, Role.find(2).position |
|
| 209 |
end |
|
| 210 |
|
|
| 211 |
def test_move_lowest |
|
| 212 |
put :update, :id => 2, :role => {:move_to => 'lowest'}
|
|
| 213 |
assert_redirected_to '/roles' |
|
| 214 |
assert_equal Role.count, Role.find(2).position |
|
| 215 |
end |
|
| 216 |
end |
|
Also available in: Unified diff