account.rb 1.95 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3 4 5 6
# == Account role
#
# Describe behaviour of User in application
#
# Used by User
#
7 8 9 10 11 12
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._\@+-]*$
13
  def identifier
14 15 16
    # Replace non-word chars with underscores, then make sure it starts with
    # valid chars
    email.gsub(/\W/, '_').gsub(/\A([\W\_])+/, '')
17 18 19 20 21 22 23 24 25 26 27
  end

  def is_admin?
    admin
  end

  def require_ssh_key?
    keys.count == 0
  end

  def can_create_project?
28
    projects_limit > personal_projects.count
29 30
  end

31 32 33 34
  def can_create_group?
    is_admin?
  end

35 36 37 38 39 40 41 42 43 44 45 46
  def abilities
    @abilities ||= begin
                     abilities = Six.new
                     abilities << Ability
                     abilities
                   end
  end

  def can? action, subject
    abilities.allowed?(self, action, subject)
  end

47 48 49 50 51 52 53 54 55
  def last_activity_project
    projects.first
  end

  def first_name
    name.split.first unless name.blank?
  end

  def cared_merge_requests
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
56
    MergeRequest.where("author_id = :id or assignee_id = :id", id: self.id)
57 58 59 60 61
  end

  # Remove user from all projects and
  # set blocked attribute to true
  def block
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
62
    users_projects.find_each do |membership|
63 64 65 66 67 68 69 70 71 72 73
      return false unless membership.destroy
    end

    self.blocked = true
    save
  end

  def projects_limit_percent
    return 100 if projects_limit.zero?
    (my_own_projects.count.to_f / projects_limit) * 100
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
74 75

  def recent_push project_id = nil
randx's avatar
randx committed
76 77
    # Get push events not earlier than 2 hours ago
    events = recent_events.code_push.where("created_at > ?", Time.now - 2.hours)
78
    events = events.where(project_id: project_id) if project_id
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
79 80 81 82

    # Take only latest one
    events = events.recent.limit(1).first
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
83

84
  def projects_sorted_by_activity
85
    authorized_projects.sorted_by_activity
86 87 88 89 90 91 92 93 94
  end

  def several_namespaces?
    namespaces.size > 1
  end

  def namespace_id
    namespace.try :id
  end
95
end