Class: DeployKey

Inherits:
Key show all
Includes:
FromUnion, IgnorableColumns
Defined in:
app/models/deploy_key.rb

Constant Summary

Constants included from Expirable

Expirable::DAYS_TO_EXPIRE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Key

#add_to_authorized_keys, #can_delete?, #key=, #post_create_hook, #post_destroy_hook, #projects, #public_key, #publishable_key, #refresh_user_cache, regular_keys, #remove_from_authorized_keys, #shell_id, #update_last_used_at

Methods included from Expirable

#expired?, #expires?, #expires_soon?

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, where_exists, with_fast_statement_timeout, without_order

Class Method Details

.with_write_access_for_project(project, deploy_key: nil) ⇒ Object


60
61
62
63
64
65
# File 'app/models/deploy_key.rb', line 60

def self.with_write_access_for_project(project, deploy_key: nil)
  query = in_projects(project).with_write_access
  query = query.where(id: deploy_key) if deploy_key

  query
end

Instance Method Details

#almost_orphaned?Boolean

Returns:

  • (Boolean)

28
29
30
# File 'app/models/deploy_key.rb', line 28

def almost_orphaned?
  self.deploy_keys_projects.size == 1
end

#can_push_to?(project) ⇒ Boolean

Returns:

  • (Boolean)

44
45
46
# File 'app/models/deploy_key.rb', line 44

def can_push_to?(project)
  !!deploy_keys_project_for(project)&.can_push?
end

#deploy_keys_project_for(project) ⇒ Object


48
49
50
51
52
53
54
# File 'app/models/deploy_key.rb', line 48

def deploy_keys_project_for(project)
  if association(:deploy_keys_projects).loaded?
    deploy_keys_projects.find { |dkp| dkp.project_id.eql?(project&.id) }
  else
    deploy_keys_projects.find_by(project: project)
  end
end

#destroyed_when_orphaned?Boolean

Returns:

  • (Boolean)

32
33
34
# File 'app/models/deploy_key.rb', line 32

def destroyed_when_orphaned?
  self.private?
end

#has_access_to?(project) ⇒ Boolean

Returns:

  • (Boolean)

40
41
42
# File 'app/models/deploy_key.rb', line 40

def has_access_to?(project)
  deploy_keys_project_for(project).present?
end

#orphaned?Boolean

Returns:

  • (Boolean)

24
25
26
# File 'app/models/deploy_key.rb', line 24

def orphaned?
  self.deploy_keys_projects.empty?
end

#private?Boolean

Returns:

  • (Boolean)

20
21
22
# File 'app/models/deploy_key.rb', line 20

def private?
  !public?
end

#projects_with_write_accessObject


56
57
58
# File 'app/models/deploy_key.rb', line 56

def projects_with_write_access
  Project.with_route.where(id: deploy_keys_projects.with_write_access.select(:project_id))
end

#userObject


36
37
38
# File 'app/models/deploy_key.rb', line 36

def user
  super || User.ghost
end