Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#48619] Users are able to create multiple API access tokens

Andreas Pfohld52a0264a94a
diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb
index 0b698f5a010..a01c556c161 100644
--- a/app/controllers/my_controller.rb
+++ b/app/controllers/my_controller.rb
@@ -172,7 +172,7 @@ class MyController < ApplicationController
end
def revoke_api_key
- current_user.api_token.destroy
+ current_user.api_tokens.find(params[:token_id]).destroy
flash[:info] = t("my.access_token.notice_api_token_revoked")
rescue StandardError => e
Rails.logger.error "Failed to revoke api token ##{current_user.id}: #{e}"
diff --git a/app/models/token/api.rb b/app/models/token/api.rb
index 518804b038a..860019700d4 100644
--- a/app/models/token/api.rb
+++ b/app/models/token/api.rb
@@ -28,5 +28,10 @@
module Token
class API < HashedToken
+ private
+
+ def single_value?
+ false
+ end
end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index dd3c1aaa3c3..191049c27de 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -54,7 +54,7 @@ class User < Principal
dependent: :destroy,
inverse_of: :user
has_one :rss_token, class_name: "::Token::RSS", dependent: :destroy
- has_one :api_token, class_name: "::Token::API", dependent: :destroy
+ has_many :api_tokens, class_name: "::Token::API", dependent: :destroy
# The user might have one invitation token
has_one :invitation_token, class_name: "::Token::Invitation", dependent: :destroy
@@ -157,6 +157,7 @@ class User < Principal
def self.unique_attribute
:login
end
+
prepend ::Mixins::UniqueFinder
def current_password
@@ -279,12 +280,12 @@ class User < Principal
# Don't forget to check columns_for_name
case formatter || Setting.user_format
- when :firstname_lastname then "#{firstname} #{lastname}"
- when :lastname_firstname then "#{lastname} #{firstname}"
- when :lastname_n_firstname then "#{lastname}#{firstname}"
+ when :firstname_lastname then "#{firstname} #{lastname}"
+ when :lastname_firstname then "#{lastname} #{firstname}"
+ when :lastname_n_firstname then "#{lastname}#{firstname}"
when :lastname_coma_firstname then "#{lastname}, #{firstname}"
- when :firstname then firstname
- when :username then login
+ when :firstname then firstname
+ when :username then login
else
"#{firstname} #{lastname}"
@@ -344,7 +345,7 @@ class User < Principal
# Does the backend storage allow this user to change their password?
def change_password_allowed?
return false if uses_external_authentication? ||
- OpenProject::Configuration.disable_password_login?
+ OpenProject::Configuration.disable_password_login?
ldap_auth_source_id.blank?
end
@@ -376,7 +377,7 @@ class User < Principal
return false if block_threshold == 0 # disabled
last_failed_login_within_block_time? and
- failed_login_count >= block_threshold
+ failed_login_count >= block_threshold
end
def log_failed_login
@@ -526,22 +527,23 @@ class User < Principal
# Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only
# one anonymous user per database.
def self.anonymous # rubocop:disable Metrics/AbcSize
- RequestStore[:anonymous_user] ||= begin
- anonymous_user = AnonymousUser.first
-
- if anonymous_user.nil?
- (anonymous_user = AnonymousUser.new.tap do |u|
- u.lastname = "Anonymous"
- u.login = ""
- u.firstname = ""
- u.mail = ""
- u.status = User.statuses[:active]
- end).save
-
- raise "Unable to create the anonymous user." if anonymous_user.new_record?
+ RequestStore[:anonymous_user] ||=
+ begin
+ anonymous_user = AnonymousUser.first
+
+ if anonymous_user.nil?
+ (anonymous_user = AnonymousUser.new.tap do |u|
+ u.lastname = "Anonymous"
+ u.login = ""
+ u.firstname = ""
+ u.mail = ""
+ u.status = User.statuses[:active]
+ end).save
+
+ raise "Unable to create the anonymous user." if anonymous_user.new_record?
+ end
+ anonymous_user
end
- anonymous_user
- end
end
def self.system
diff --git a/app/views/my/access_token.html.erb b/app/views/my/access_token.html.erb
index b9700bd481f..6adc3b8c953 100644
--- a/app/views/my/access_token.html.erb
+++ b/app/views/my/access_token.html.erb
@@ -35,16 +35,16 @@ See COPYRIGHT and LICENSE files for more details.
<%= toolbar title: I18n.t('my_account.access_tokens.access_tokens') %>
<%= render partial: 'my/access_token_partials/api_tokens_section',
- locals: { api_token: @user.api_token }
+ locals: { api_tokens: @user.api_tokens }
%>
- <%= render partial: 'my/access_token_partials/icalendar_tokens_section',
- locals: { ical_tokens_grouped_by_query: @ical_tokens_grouped_by_query }
+ <%= render partial: 'my/access_token_partials/icalendar_tokens_section',
+ locals: { ical_tokens_grouped_by_query: @ical_tokens_grouped_by_query }
%>
- <%= render partial: 'my/access_token_partials/oauth_tokens_section',
- locals: { granted_applications: granted_applications }
+ <%= render partial: 'my/access_token_partials/oauth_tokens_section',
+ locals: { granted_applications: granted_applications }
%>
- <%= render partial: 'my/access_token_partials/rss_tokens_section',
- locals: { rss_token: @user.rss_token }
+ <%= render partial: 'my/access_token_partials/rss_tokens_section',
+ locals: { rss_token: @user.rss_token }
%>
<%= render partial: 'my/access_token_partials/storage_tokens_section',
locals: { storage_tokens: @storage_tokens }
diff --git a/app/views/my/access_token_partials/_api_tokens_section.html.erb b/app/views/my/access_token_partials/_api_tokens_section.html.erb
index 8d2228cf130..b4ca48681e6 100644
--- a/app/views/my/access_token_partials/_api_tokens_section.html.erb
+++ b/app/views/my/access_token_partials/_api_tokens_section.html.erb
@@ -37,20 +37,21 @@ See COPYRIGHT and LICENSE files for more details.
<%= t("my_account.access_tokens.api.text_hint") %>
</p>
<% if Setting.rest_api_enabled? %>
- <% if api_token %>
+ <% if api_tokens.any? %>
<div class="generic-table--container">
<div class="generic-table--results-container">
<table class="generic-table">
- <%= render partial: "my/access_token_partials/token_table_header",
- locals: {
- column_headers: [
- t('attributes.name'),
- User.human_attribute_name(:created_at),
- t('my_account.access_tokens.headers.expiration')
- ]
- }
+ <%= render partial: "my/access_token_partials/token_table_header",
+ locals: {
+ column_headers: [
+ t('attributes.name'),
+ User.human_attribute_name(:created_at),
+ t('my_account.access_tokens.headers.expiration')
+ ]
+ }
%>
<tbody>
+ <% api_tokens.each do |api_token| %>
<tr>
<td class="-w-rel-60" data-test-selector="api-token-row-name"><%= t("my_account.access_tokens.api.static_token_name") %></td>
<td>
@@ -61,21 +62,25 @@ See COPYRIGHT and LICENSE files for more details.
<td><%= t('my_account.access_tokens.indefinite_expiration') %></td>
<td class="buttons">
<%= link_to "",
- { action: 'revoke_api_key' },
+ {
+ controller: :my,
+ action: 'revoke_api_key',
+ token_id: api_token.id
+ },
method: :delete,
data: { confirm: t('my_account.access_tokens.simple_revoke_confirmation'), test_selector: 'api-token-revoke' },
class: 'icon icon-delete' %>
</td>
</tr>
+ <% end %>
</tbody>
</table>
</div>
</div>
- <% else %>
- <%= link_to({ action: 'generate_api_key' }, method: :post, class: 'spot-link', data: { test_selector: 'api-token-add' }) do %>
- <span class="spot-icon spot-icon_add"></span>
- <span><%= t('my_account.access_tokens.api.static_token_name') %></span>
- <% end %>
+ <% end %>
+ <%= link_to({ action: 'generate_api_key' }, method: :post, class: 'spot-link', data: { test_selector: 'api-token-add' }) do %>
+ <span class="spot-icon spot-icon_add"></span>
+ <span><%= t('my_account.access_tokens.api.static_token_name') %></span>
<% end %>
<% else %>
<div tabindex="0" class="-info op-toast">