File: render_access_tokens.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (52 lines) | stat: -rw-r--r-- 1,394 bytes parent folder | download
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
# frozen_string_literal: true

module RenderAccessTokens
  extend ActiveSupport::Concern

  def active_access_tokens
    tokens = finder(state: 'active', sort: 'expires_at_asc_id_desc').execute.preload_users
    size = tokens.size

    tokens = tokens.page(page)
    add_pagination_headers(tokens)

    [represent(tokens), size]
  end

  def inactive_access_tokens
    tokens = finder(state: 'inactive', sort: 'updated_at_desc').execute.preload_users

    # We don't call `add_pagination_headers` as this overrides the
    # pagination of active tokens.

    represent(tokens)
  end

  def add_pagination_headers(relation)
    Gitlab::Pagination::OffsetHeaderBuilder.new(
      request_context: self,
      per_page: relation.limit_value,
      page: relation.current_page,
      next_page: relation.next_page,
      prev_page: relation.prev_page,
      total: relation.total_count,
      params: params.permit(:page, :per_page)
    ).execute
  end

  def page
    (pagination_params[:page] || 1).to_i
  end

  def expiry_ics(tokens)
    cal = Icalendar::Calendar.new
    tokens.each do |token|
      cal.event do |event|
        event.dtstart = Icalendar::Values::Date.new(token[:expires_at].delete('-'))
        event.dtend = Icalendar::Values::Date.new(token[:expires_at].delete('-'))
        event.summary = "Token #{token[:name]} expires today"
      end
    end
    cal.to_ical
  end
end