Skip to content

Commit

Permalink
[api][webui][ci] Token class now have subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
Moises Deniz Aleman authored and hennevogel committed Jun 29, 2017
1 parent a03c953 commit 300ff93
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/api/app/controllers/person_controller.rb
Expand Up @@ -266,7 +266,7 @@ def change_password(login, password)
# GET /person/<login>/token
def tokenlist
user = User.get_by_login(params[:login])
@list = user.tokens
@list = user.service_tokens
end

# POST /person/<login>/token
Expand All @@ -280,7 +280,7 @@ def command_token
if params[:project] || params[:package]
pkg = Package.get_by_project_and_name( params[:project], params[:package] )
end
@token = Token.create( user: user, package: pkg )
@token = Token::Service.create( user: user, package: pkg )
end

class TokenNotFound < APIException
Expand All @@ -291,7 +291,7 @@ class TokenNotFound < APIException
def delete_token
user = User.get_by_login(params[:login])

token = Token.where( user_id: user.id, id: params[:id] ).first
token = Token::Service.where( user_id: user.id, id: params[:id] ).first
raise TokenNotFound, "Specified token \"#{params[:id]}\" got not found" unless token
token.destroy
render_ok
Expand Down
2 changes: 1 addition & 1 deletion src/api/app/controllers/trigger_controller.rb
Expand Up @@ -19,7 +19,7 @@ def runservice
return
end

token = Token.find_by_string auth[6..-1]
token = Token::Service.find_by_string(auth[6..-1])

unless token
render_error message: "Token not found", status: 404
Expand Down
12 changes: 9 additions & 3 deletions src/api/app/controllers/webui/feeds_controller.rb
Expand Up @@ -42,8 +42,14 @@ def commits
end

def notifications
@configuration = ::Configuration.first
@user = User.current
@notifications = User.current.combined_rss_feed_items
token = Token::Rss.find_by_string(params[:token])
if token
@configuration = ::Configuration.first
@user = token.user
@notifications = token.user.combined_rss_feed_items
else
flash[:error] = "Unknown Token for RSS feed"
redirect_back(fallback_location: root_path)
end
end
end
1 change: 0 additions & 1 deletion src/api/app/models/event_subscription.rb
Expand Up @@ -60,7 +60,6 @@ def enabled?
# created_at :datetime
# updated_at :datetime
# group_id :integer indexed
# receive :boolean
# channel :integer default("disabled"), not null
#
# Indexes
Expand Down
4 changes: 2 additions & 2 deletions src/api/app/models/package.rb
Expand Up @@ -124,9 +124,9 @@ class PutFileNoPermission < APIException; setup 403; end
validate :valid_name

has_one :backend_package, foreign_key: :package_id, dependent: :destroy, inverse_of: :package
has_one :token, foreign_key: :package_id, dependent: :destroy
has_one :token, class_name: 'Token::Service', foreign_key: :package_id, dependent: :destroy

has_many :tokens, dependent: :destroy, inverse_of: :package
has_many :tokens, class_name: 'Token::Service', dependent: :destroy, inverse_of: :package

def self.check_access?(package)
return false if package.nil?
Expand Down
20 changes: 4 additions & 16 deletions src/api/app/models/token.rb
@@ -1,23 +1,10 @@
class Token < ApplicationRecord
belongs_to :user, foreign_key: 'user_id', inverse_of: :tokens
belongs_to :user, foreign_key: 'user_id', inverse_of: :service_tokens
belongs_to :package, inverse_of: :tokens

validates :user_id, presence: true
after_create :update_token

def self.find_by_string(token)
token = Token.where(string: token.to_s).includes(:package, :user).first
return unless token && token.user_id
has_secure_token :string

# package found and user has write access
token
end

def update_token
# base64 with a length that is a multiple of 3 avoids trailing "=" chars
self.string = SecureRandom.base64(30) # 30 bytes leads to 40 chars string
save!
end
validates :user_id, presence: true
end

# == Schema Information
Expand All @@ -28,6 +15,7 @@ def update_token
# string :string(255) indexed
# user_id :integer not null, indexed
# package_id :integer indexed
# type :string(255)
#
# Indexes
#
Expand Down
24 changes: 24 additions & 0 deletions src/api/app/models/token/rss.rb
@@ -0,0 +1,24 @@
class Token::Rss < Token
end

# == Schema Information
#
# Table name: tokens
#
# id :integer not null, primary key
# string :string(255) indexed
# user_id :integer not null, indexed
# package_id :integer indexed
# type :string(255)
#
# Indexes
#
# index_tokens_on_string (string) UNIQUE
# package_id (package_id)
# user_id (user_id)
#
# Foreign Keys
#
# tokens_ibfk_1 (user_id => users.id)
# tokens_ibfk_2 (package_id => packages.id)
#
24 changes: 24 additions & 0 deletions src/api/app/models/token/service.rb
@@ -0,0 +1,24 @@
class Token::Service < Token
end

# == Schema Information
#
# Table name: tokens
#
# id :integer not null, primary key
# string :string(255) indexed
# user_id :integer not null, indexed
# package_id :integer indexed
# type :string(255)
#
# Indexes
#
# index_tokens_on_string (string) UNIQUE
# package_id (package_id)
# user_id (user_id)
#
# Foreign Keys
#
# tokens_ibfk_1 (user_id => users.id)
# tokens_ibfk_2 (package_id => packages.id)
#
3 changes: 2 additions & 1 deletion src/api/app/models/user.rb
Expand Up @@ -36,7 +36,8 @@ class User < ApplicationRecord
has_many :comments, dependent: :destroy, inverse_of: :user
has_many :status_messages
has_many :messages
has_many :tokens, dependent: :destroy, inverse_of: :user
has_many :service_tokens, class_name: 'Token::Service', dependent: :destroy, inverse_of: :user
has_one :rss_token, class_name: 'Token::Rss', dependent: :destroy

has_many :reviews, dependent: :nullify, as: :reviewable

Expand Down
5 changes: 5 additions & 0 deletions src/api/db/migrate/20170621083718_add_st_ito_tokens.rb
@@ -0,0 +1,5 @@
class AddStItoTokens < ActiveRecord::Migration[5.0]
def change
add_column :tokens, :type, :string
end
end
8 changes: 6 additions & 2 deletions src/api/db/structure.sql
Expand Up @@ -1094,6 +1094,7 @@ CREATE TABLE `tokens` (
`string` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_id` int(11) NOT NULL,
`package_id` int(11) DEFAULT NULL,
`type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_tokens_on_string` (`string`) USING BTREE,
KEY `user_id` (`user_id`) USING BTREE,
Expand Down Expand Up @@ -1476,6 +1477,8 @@ INSERT INTO `schema_migrations` (version) VALUES
('20170516140442'),
('20170607110443'),
('20170614083014'),
('20170619111734'),
('20170621083718'),
('20170621100321'),
('20170621103748'),
('20170628115727'),
Expand Down Expand Up @@ -1513,5 +1516,6 @@ INSERT INTO `schema_migrations` (version) VALUES
('6'),
('7'),
('8'),
('9'),
('20170619111734');
('9');


4 changes: 2 additions & 2 deletions src/api/test/functional/source_services_test.rb
Expand Up @@ -498,12 +498,12 @@ def test_run_service_via_token
assert_response :success
doc = REXML::Document.new(@response.body)
alltoken = doc.elements['//data'].text
assert_equal 40, alltoken.length
assert_equal 24, alltoken.length
post '/person/tom/token?cmd=create&project=home:tom&package=service'
assert_response :success
doc = REXML::Document.new(@response.body)
token = doc.elements['//data'].text
assert_equal 40, token.length
assert_equal 24, token.length

# ANONYMOUS
reset_auth
Expand Down

0 comments on commit 300ff93

Please sign in to comment.