2012-12-30 15:19:31 +01:00
|
|
|
# == Account role
|
|
|
|
#
|
|
|
|
# Describe behaviour of User in application
|
|
|
|
#
|
|
|
|
# Used by User
|
|
|
|
#
|
2012-09-04 06:04:36 +02:00
|
|
|
module Account
|
|
|
|
# Returns a string for use as a Gitolite user identifier
|
|
|
|
#
|
|
|
|
# Note that Gitolite 2.x requires the following pattern for users:
|
|
|
|
#
|
|
|
|
# ^@?[0-9a-zA-Z][0-9a-zA-Z._\@+-]*$
|
2012-06-07 14:44:57 +02:00
|
|
|
def identifier
|
2012-09-04 06:04:36 +02:00
|
|
|
# Replace non-word chars with underscores, then make sure it starts with
|
|
|
|
# valid chars
|
|
|
|
email.gsub(/\W/, '_').gsub(/\A([\W\_])+/, '')
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_admin?
|
|
|
|
admin
|
|
|
|
end
|
|
|
|
|
|
|
|
def require_ssh_key?
|
|
|
|
keys.count == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_create_project?
|
2013-01-02 18:00:00 +01:00
|
|
|
projects_limit > personal_projects.count
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
2012-10-09 08:46:20 +02:00
|
|
|
def can_create_group?
|
|
|
|
is_admin?
|
|
|
|
end
|
|
|
|
|
2012-11-25 10:57:01 +01:00
|
|
|
def abilities
|
|
|
|
@abilities ||= begin
|
|
|
|
abilities = Six.new
|
|
|
|
abilities << Ability
|
|
|
|
abilities
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can? action, subject
|
|
|
|
abilities.allowed?(self, action, subject)
|
|
|
|
end
|
|
|
|
|
2012-06-07 14:44:57 +02:00
|
|
|
def last_activity_project
|
|
|
|
projects.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_name
|
|
|
|
name.split.first unless name.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
def cared_merge_requests
|
2012-12-22 10:59:05 +01:00
|
|
|
MergeRequest.where("author_id = :id or assignee_id = :id", id: self.id)
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Remove user from all projects and
|
|
|
|
# set blocked attribute to true
|
|
|
|
def block
|
2012-10-09 02:10:04 +02:00
|
|
|
users_projects.find_each do |membership|
|
2012-06-07 14:44:57 +02:00
|
|
|
return false unless membership.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
self.blocked = true
|
|
|
|
save
|
|
|
|
end
|
|
|
|
|
|
|
|
def projects_limit_percent
|
|
|
|
return 100 if projects_limit.zero?
|
2013-01-02 18:32:34 +01:00
|
|
|
(personal_projects.count.to_f / projects_limit) * 100
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
2012-06-12 16:43:16 +02:00
|
|
|
|
|
|
|
def recent_push project_id = nil
|
2012-06-21 17:41:22 +02:00
|
|
|
# Get push events not earlier than 2 hours ago
|
|
|
|
events = recent_events.code_push.where("created_at > ?", Time.now - 2.hours)
|
2012-08-11 00:07:50 +02:00
|
|
|
events = events.where(project_id: project_id) if project_id
|
2012-06-12 16:43:16 +02:00
|
|
|
|
|
|
|
# Take only latest one
|
|
|
|
events = events.recent.limit(1).first
|
|
|
|
end
|
2012-07-20 07:39:34 +02:00
|
|
|
|
2012-11-16 19:31:17 +01:00
|
|
|
def projects_sorted_by_activity
|
2013-01-02 18:00:00 +01:00
|
|
|
authorized_projects.sorted_by_activity
|
2012-11-25 10:57:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def several_namespaces?
|
|
|
|
namespaces.size > 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def namespace_id
|
|
|
|
namespace.try :id
|
|
|
|
end
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|