summaryrefslogtreecommitdiff
path: root/app/models/user.rb
blob: 6c16035eadece27ae83bd29af8286fc60dc08e52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# User object is stored in session
class User
  attr_reader :attributes

  def initialize(hash)
    @attributes = hash
  end

  def gitlab_projects(page = 1, per_page = 100)
    Rails.cache.fetch(cache_key(page, per_page)) do
      Project.from_gitlab(self, page, per_page, :authorized)
    end
  end

  def method_missing(meth, *args, &block)
    if attributes.has_key?(meth.to_s)
      attributes[meth.to_s]
    else
      super
    end
  end

  def cache_key(*args)
    "#{self.id}:#{args.join(":")}:#{sync_at.to_s}"
  end

  def sync_at
    @sync_at ||= Time.now
  end

  def reset_cache
    @sync_at = Time.now
  end

  def can_access_project?(project_gitlab_id)
    opts = {
      private_token: self.private_token,
    }

    Rails.cache.fetch(cache_key(project_gitlab_id, sync_at)) do
      !!Network.new.project(self.url, opts, project_gitlab_id)
    end
  end

  def can_manage_project?(project_gitlab_id)
    opts = {
      private_token: self.private_token,
    }

    Rails.cache.fetch(cache_key('manage', project_gitlab_id, sync_at)) do
      !!Network.new.project_hooks(self.url, opts, project_gitlab_id)
    end
  end
end