OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/app/models/principal.rb

185 lines
4.8 KiB

#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# 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.
#
# See docs/COPYRIGHT.rdoc for more details.
#++
class Principal < ApplicationRecord
include ::Scopes::Scoped
# Account statuses
enum status: {
active: 1,
registered: 2,
locked: 3,
invited: 4
}.freeze
self.table_name = "#{table_name_prefix}users#{table_name_suffix}"
has_one :preference,
dependent: :destroy,
class_name: 'UserPreference',
foreign_key: 'user_id'
has_many :members, foreign_key: 'user_id', dependent: :destroy
has_many :memberships, -> {
includes(:project, :roles)
.where(["projects.active = ? OR project_id IS NULL", true])
.order(Arel.sql('projects.name ASC'))
# haven't been able to produce the order using hashes
},
class_name: 'Member',
foreign_key: 'user_id'
has_many :projects, through: :memberships
has_many :categories, foreign_key: 'assigned_to_id', dependent: :nullify
scopes :like,
:human,
:not_builtin,
:possible_assignee,
:possible_member,
:user,
:ordered_by_name
scope :not_locked, -> {
not_builtin.where.not(status: statuses[:locked])
}
scope :in_project, ->(project) {
where(id: Member.of(project).select(:user_id))
}
scope :not_in_project, ->(project) {
where.not(id: Member.of(project).select(:user_id))
}
Placeholder user services and administration (#8944) * Adding placeholder user contracts * Adding create, update, and delete services for placeholder users * WIP: Adding Placeholder User contract specs [ci skip] * Extract contract validation into common helper * Add common validation in BaseContract + common example for admin checks * Introduce common ModelContract shared context for validations * WIP: PlaceholderUser controller, i18n, and routes [ci skip] * Placeholder users index page and query - moved all group related scopes from User to Principal to make them also available in PlaceholderUser. * end * Create PlaceholderUser * Feature spec for editing a placeholder user * Manage PlaceholderUser memberships The managment of memberships is pretty similar for User and PlaceholderUser. This commit extacts the similarities and uses them for both. * General partial and show view for PlaceholderUser * Delete obosolete partial * Allow RequireAdminGuard to be used as a module function * Fix I18n for confirmation text * Smaller code improvements * Fix: Syntax for accessing status enums was wrong. * Use UpdateService for updating a placeholder user * Add spec for PlaceholderUsersController * First code improvements after code review. - more improvements to come. * Further code improvements after review ... still more to come * Correct namespace of delete service * Fix: Make placeholder user contract validate * Remove :type attribute from base contract of User and PlaceholerUser ...and add it to the CreateContracts. Also add type validations. Further extract shared examples for placeholder user attribute validation * Refactor: Extract membership hook calls to helper * Fix redirect paths for membership controllers * Specs already present in shared exampels. * Fix duplicates routes for users and placeholder users * Fix user path * Add attribute name and lastname We don't need a writeable check as both are equally writable * Replace more references to tab_edit_user_path * Skip specs for PlaceholderUsers::DeletionService We will tackle that service in a separate PR. * Fix module usage of RequireAdminGuard * Fix group filter for placeholder users * Fix invalid reference to expect_valid * Fix: Fix tabbed edit path for placeholder users * Fix status filtering on users * Linting * Improve generalisation of individual principal filter cell - Check for presence of groups and statuses in order to toggle visibility of their UI element. - Remove groups from placeholder user controller and cell initialization and options * Fix selector on groups assign * Remove using_shared_fixtures Co-authored-by: Oliver Günther <mail@oliverguenther.de>
4 years ago
scope :in_group, ->(group) {
within_group(group)
}
scope :not_in_group, ->(group) {
within_group(group, false)
}
scope :within_group, ->(group, positive = true) {
group_id = group.is_a?(Group) ? [group.id] : Array(group).map(&:to_i)
sql_condition = group_id.any? ? 'WHERE gu.group_id IN (?)' : ''
sql_not = positive ? '' : 'NOT'
sql_query = [
"#{User.table_name}.id #{sql_not} IN " \
"(SELECT gu.user_id FROM #{table_name_prefix}group_users#{table_name_suffix} gu #{sql_condition})"
]
if group_id.any?
sql_query.push group_id
end
where(sql_query)
}
before_create :set_default_empty_values
def name(_formatter = nil)
to_s
end
def self.search_scope_without_project(project, query)
not_locked.like(query).not_in_project(project)
end
def self.me
where(id: User.current.id)
end
def self.in_visible_project(user = User.current)
in_project(Project.visible(user))
end
def self.in_visible_project_or_me(user = User.current)
in_visible_project(user)
.or(me)
end
# Helper method to identify internal users
def builtin?
false
end
##
# Allows the API and other sources to determine locking actions
# on represented collections of children of Principals.
# Must be overridden by User
def lockable?
false
end
##
# Allows the API and other sources to determine unlocking actions
# on represented collections of children of Principals.
# Must be overridden by User
def activatable?
false
end
def <=>(other)
if instance_of?(other.class)
to_s.downcase <=> other.to_s.downcase
else
# groups after users
other.class.name <=> self.class.name
end
end
class << self
# Hack to exclude the Users::InexistentUser
# from showing up on filters for type.
# The method is copied over from rails changed only
# by the #compact call.
def type_condition(table = arel_table)
sti_column = table[inheritance_column]
sti_names = ([self] + descendants).map(&:sti_name).compact
predicate_builder.build(sti_column, sti_names)
end
end
protected
# Make sure we don't try to insert NULL values (see #4632)
def set_default_empty_values
self.login ||= ''
self.firstname ||= ''
self.lastname ||= ''
self.mail ||= ''
true
end
extend Pagination::Model
end