Skip to content

Commit 3198d21

Browse files
reshetovmr-exz
andauthored
Removed redundant gem && code improvements (#97)
Co-authored-by: Nik Il <root@exzec.ru>
1 parent 0bbcef7 commit 3198d21

File tree

8 files changed

+38
-25
lines changed

8 files changed

+38
-25
lines changed

Gemfile

-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ group :test, :development do
88
gem 'byebug'
99
gem 'capybara-screenshot'
1010
end
11-

app/controllers/keys_controller.rb

+12-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class KeysController < ApplicationController
1010
helper ContextMenusHelper
1111

1212
def index
13-
1413
unless Setting.plugin_vault['use_redmine_encryption'] ||
1514
Setting.plugin_vault['use_null_encryption']
1615
if not Setting.plugin_vault['encryption_key'] or Setting.plugin_vault['encryption_key'].empty?
@@ -37,9 +36,10 @@ def index
3736
end
3837

3938
@keys = @keys.order(sort_clause) unless @keys.nil?
40-
@keys = @keys.select { |key| key.whitelisted?(User, @project) } unless @keys.nil?
41-
@keys = [] if @keys.nil? # hack for decryption
4239

40+
@keys = @keys.select { |key| key.whitelisted?(User.current, @project) } unless @keys.nil?
41+
@keys = [] if @keys.nil? #hack for decryption
42+
4343
@limit = per_page_option
4444
@key_count = @keys.count
4545
@key_pages = Paginator.new @key_count, @limit, params[:page]
@@ -95,8 +95,9 @@ def all
9595
end
9696

9797
@keys = @keys.order(sort_clause) unless @keys.nil?
98-
@keys = @keys.select { |key| key.whitelisted?(User, key.project) } unless @keys.nil?
99-
@keys = [] if @keys.nil? # hack for decryption
98+
99+
@keys = @keys.select { |key| key.whitelisted?(User.current, key.project) } unless @keys.nil?
100+
@keys = [] if @keys.nil? #hack for decryption
100101

101102
@limit = per_page_option
102103
@key_count = @keys.count
@@ -129,10 +130,10 @@ def copy
129130

130131
def create
131132
save_file if key_params[:file]
132-
@key = Vault::Key.new(key_params)
133-
133+
@key = Vault::Key.new
134+
@key.safe_attributes = key_params.except(:tags)
134135
@key.project = @project
135-
136+
136137
self.update_wishlist
137138

138139
respond_to do |format|
@@ -148,6 +149,7 @@ def update
148149
save_file if key_params[:file]
149150
respond_to do |format|
150151
self.update_wishlist
152+
@key.safe_attributes = key_params.except(:tags)
151153

152154
if @key.update(key_params)
153155
@key.tags = key_params[:tags]
@@ -169,7 +171,7 @@ def update_wishlist
169171
end
170172

171173
def edit
172-
if !@key.whitelisted?(User, @project)
174+
if !@key.whitelisted?(User.current, @project)
173175
render_error t("error.key.not_whitelisted")
174176
return
175177
else
@@ -181,7 +183,7 @@ def edit
181183
end
182184

183185
def show
184-
if !@key.whitelisted?(User, @project)
186+
if !@key.whitelisted?(User.current, @project)
185187
render_error t("error.key.not_whitelisted")
186188
return
187189
else

app/controllers/tags_controller.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ def index
88
end
99

1010
def create
11-
@tag = @key.tags.build(tag_params)
11+
@tag = @key.tags.build
12+
@tag.safe_attributes = tag_params
13+
1214
if @tag.save
1315
redirect_to project_key_tags_path(@project, @key), notice: 'Tag was successfully created.'
1416
else
@@ -17,7 +19,8 @@ def create
1719
end
1820

1921
def update
20-
if @tag.update(tag_params)
22+
@tag.safe_attributes = tag_params
23+
if @tag.save
2124
redirect_to project_key_tags_path(@project, @key), notice: 'Tag was successfully updated.'
2225
else
2326
render :index
@@ -46,4 +49,4 @@ def find_tag
4649
def tag_params
4750
params.require(:tag).permit(:name, :color)
4851
end
49-
end
52+
end

app/models/vault/key.rb

+13-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ module Vault
33
require 'iconv'
44

55
class Vault::Key < ActiveRecord::Base
6+
include Redmine::SafeAttributes
7+
68
belongs_to :project
79
has_and_belongs_to_many :tags, join_table: 'keys_vault_tags'
810

11+
safe_attributes 'project_id', 'name', 'body', 'login', 'type', 'file', 'url', 'comment', 'whitelist'
12+
913
def tags=(tags_string)
1014
tag_objects = Vault::Tag.create_from_string(tags_string)
1115
self.tags.clear
@@ -42,7 +46,6 @@ def self.import(file)
4246
whitelist: rhash['comment']
4347
).update_column(:id, rhash['id'])
4448
rescue
45-
4649
end
4750
else
4851
begin
@@ -66,16 +69,19 @@ def self.import(file)
6669
end
6770

6871
def whitelisted?(user, project)
69-
return true if user.current.admin or !user.current.allowed_to?(:whitelist_keys, project)
70-
self.whitelist.split(",").each do |id|
71-
return true if User.in_group(id).where(:id => user.current.id).count == 1
72+
return true if user.admin || !user.allowed_to?(:whitelist_keys, project)
73+
74+
whitelist_ids = self.whitelist.split(',')
75+
return true if whitelist_ids.include?(user.id.to_s)
76+
77+
whitelist_ids.each do |id|
78+
return true if User.in_group(id).where(id: user.id).any?
7279
end
73-
return self.whitelist.split(",").include?(user.current.id.to_s)
74-
end
7580

81+
false
82+
end
7683
end
7784

7885
class Vault::KeysVaultTags < ActiveRecord::Base
7986
end
80-
8187
end

app/models/vault/key_file.rb

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module Vault
22
class KeyFile < Key
33
before_update :update_file, if: :file_changed?
44
before_destroy :delete_file
5-
65
private
76

87
def update_file

app/models/vault/tag.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
module Vault
22
class Tag < ActiveRecord::Base
3+
include Redmine::SafeAttributes
4+
35
self.table_name = 'vault_tags'
4-
has_and_belongs_to_many :keys, join_table: 'keys_vault_tags'
6+
has_and_belongs_to_many :keys
7+
8+
safe_attributes 'name', 'color'
59

610
validates :name, presence: true, uniqueness: true
711
validates :color, presence: true

assets/javascripts/vault.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ $(function () {
3030
hiddenLabel.toggle();
3131
hiddenLabel.prev().toggle();
3232
}
33-
});
33+
});

config/locales/ru.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ru:
1111

1212
activerecord:
1313
models:
14-
password: "Ключь"
14+
password: "Ключ"
1515
sftp: "SFTP"
1616

1717
backups:

0 commit comments

Comments
 (0)