Merge pull request #1574 from jbargu/jg_sci_3149_3163

Valid smart annotation links of smart annotation imported within protocol [SCI-3149, 3163]
This commit is contained in:
Jure Grabnar 2019-04-01 14:32:54 +02:00 committed by GitHub
commit e165459ef5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 85 additions and 33 deletions

View file

@ -416,7 +416,7 @@ class RepositoryRowsController < ApplicationController
user: current_user.full_name, user: current_user.full_name,
column: cell.repository_column.name, column: cell.repository_column.name,
record: record.name, record: record.name,
repository: record.repository), repository: record.repository.name),
message: t('notifications.repository_annotation_message_html', message: t('notifications.repository_annotation_message_html',
record: link_to(record.name, table_url), record: link_to(record.name, table_url),
column: link_to(cell.repository_column.name, table_url)) column: link_to(cell.repository_column.name, table_url))

View file

@ -114,15 +114,15 @@ module ApplicationHelper
# sometimes happens that the "team" param gets wrong data: "{nil, []}" # sometimes happens that the "team" param gets wrong data: "{nil, []}"
# so we have to check if the "team" param is kind of Team object # so we have to check if the "team" param is kind of Team object
team = nil unless team.is_a? Team team = nil unless team.is_a? Team
new_text = smart_annotation_filter_resources(text) new_text = smart_annotation_filter_resources(text, team)
new_text = smart_annotation_filter_users(new_text, team) new_text = smart_annotation_filter_users(new_text, team)
new_text new_text
end end
# Check if text have smart annotations of resources # Check if text have smart annotations of resources
# and outputs a link to resource # and outputs a link to resource
def smart_annotation_filter_resources(text) def smart_annotation_filter_resources(text, team)
SmartAnnotations::TagToHtml.new(current_user, text).html SmartAnnotations::TagToHtml.new(current_user, team, text).html
end end
# Check if text have smart annotations of users # Check if text have smart annotations of users

View file

@ -5,31 +5,37 @@ module SmartAnnotations
class << self class << self
include Canaid::Helpers::PermissionsHelper include Canaid::Helpers::PermissionsHelper
def check(user, type, object) def check(user, team, type, object)
send("validate_#{type}_permissions", user, object) send("validate_#{type}_permissions", user, team, object)
end end
private private
def validate_prj_permissions(user, object) def validate_prj_permissions(user, team, object)
can_read_project?(user, object) object.team.id == team.id && can_read_project?(user, object)
end end
def validate_exp_permissions(user, object) def validate_exp_permissions(user, team, object)
can_read_experiment?(user, object) object.project.team.id == team.id && can_read_experiment?(user, object)
end end
def validate_tsk_permissions(user, object) def validate_tsk_permissions(user, team, object)
can_read_experiment?(user, object.experiment) object.experiment.project.team.id == team.id &&
can_read_experiment?(user, object.experiment)
end end
def validate_rep_item_permissions(user, object) def validate_rep_item_permissions(user, team, object)
return can_read_team?(user, object.repository.team) if object.repository if object.repository
return object.repository.team.id == team.id &&
can_read_team?(user, object.repository.team)
end
# handles discarded repositories # handles discarded repositories
repository = Repository.with_discarded.find_by_id(object.repository_id) repository = Repository.with_discarded.find_by_id(object.repository_id)
# evaluate to false if repository not found # evaluate to false if repository not found
return false unless repository return false unless repository
can_read_team?(user, repository.team)
repository.team.id == team && can_read_team?(user, repository.team)
end end
end end
end end

View file

@ -7,8 +7,8 @@ module SmartAnnotations
class TagToHtml class TagToHtml
attr_reader :html attr_reader :html
def initialize(user, text) def initialize(user, team, text)
parse(user, text) parse(user, team, text)
end end
private private
@ -19,7 +19,7 @@ module SmartAnnotations
tsk: MyModule, tsk: MyModule,
rep_item: RepositoryRow }.freeze rep_item: RepositoryRow }.freeze
def parse(user, text) def parse(user, team, text)
@html = text.gsub(REGEX) do |el| @html = text.gsub(REGEX) do |el|
value = extract_values(el) value = extract_values(el)
type = value[:object_type] type = value[:object_type]
@ -27,9 +27,10 @@ module SmartAnnotations
object = fetch_object(type, value[:object_id]) object = fetch_object(type, value[:object_id])
# handle repository_items edge case # handle repository_items edge case
if type == 'rep_item' if type == 'rep_item'
repository_item(value[:name], user, type, object) repository_item(value[:name], user, team, type, object)
else else
next unless object && SmartAnnotations::PermissionEval.check(user, next unless object && SmartAnnotations::PermissionEval.check(user,
team,
type, type,
object) object)
SmartAnnotations::HtmlPreview.html(nil, type, object) SmartAnnotations::HtmlPreview.html(nil, type, object)
@ -40,9 +41,10 @@ module SmartAnnotations
end end
end end
def repository_item(name, user, type, object) def repository_item(name, user, team, type, object)
if object if object
return unless SmartAnnotations::PermissionEval.check(user, type, object) return unless SmartAnnotations::PermissionEval.check(user, team, type, object)
return SmartAnnotations::HtmlPreview.html(nil, type, object) return SmartAnnotations::HtmlPreview.html(nil, type, object)
end end
SmartAnnotations::HtmlPreview.html(name, type, object) SmartAnnotations::HtmlPreview.html(name, type, object)

View file

@ -8,7 +8,7 @@ module SmartAnnotations
attr_reader :text attr_reader :text
def initialize(user, team, text) def initialize(user, team, text)
parse_items_annotations(user, text) parse_items_annotations(user, team, text)
parse_users_annotations(user, team, @text) parse_users_annotations(user, team, @text)
end end
@ -21,7 +21,7 @@ module SmartAnnotations
tsk: MyModule, tsk: MyModule,
rep_item: RepositoryRow }.freeze rep_item: RepositoryRow }.freeze
def parse_items_annotations(user, text) def parse_items_annotations(user, team, text)
@text = text.gsub(ITEMS_REGEX) do |el| @text = text.gsub(ITEMS_REGEX) do |el|
value = extract_values(el) value = extract_values(el)
type = value[:object_type] type = value[:object_type]
@ -29,9 +29,10 @@ module SmartAnnotations
object = fetch_object(type, value[:object_id]) object = fetch_object(type, value[:object_id])
# handle repository_items edge case # handle repository_items edge case
if type == 'rep_item' if type == 'rep_item'
repository_item(value[:name], user, type, object) repository_item(value[:name], user, team, type, object)
else else
next unless object && SmartAnnotations::PermissionEval.check(user, next unless object && SmartAnnotations::PermissionEval.check(user,
team,
type, type,
object) object)
SmartAnnotations::TextPreview.text(nil, type, object) SmartAnnotations::TextPreview.text(nil, type, object)
@ -52,9 +53,10 @@ module SmartAnnotations
end end
end end
def repository_item(name, user, type, object) def repository_item(name, user, team, type, object)
if object if object
return unless SmartAnnotations::PermissionEval.check(user, type, object) return unless SmartAnnotations::PermissionEval.check(user, team, type, object)
return SmartAnnotations::TextPreview.text(nil, type, object) return SmartAnnotations::TextPreview.text(nil, type, object)
end end
SmartAnnotations::TextPreview.text(name, type, object) SmartAnnotations::TextPreview.text(name, type, object)

View file

@ -5,8 +5,10 @@ describe SmartAnnotations::PermissionEval do
let(:subject) { described_class } let(:subject) { described_class }
let(:user) { create :user } let(:user) { create :user }
let(:team) { create :team } let(:team) { create :team }
let(:user_team) { create :user_team, user: user, team: team, role: 2 } let(:another_team) { create :team }
let(:project) { create :project, name: 'my project' } let!(:user_team) { create :user_team, user: user, team: team, role: :admin }
let(:project) { create :project, name: 'my project', team: team }
let!(:user_project) { create :user_project, :owner, project: project, user: user }
let(:experiment) do let(:experiment) do
create :experiment, name: 'my experiment', create :experiment, name: 'my experiment',
project: project, project: project,
@ -19,29 +21,69 @@ describe SmartAnnotations::PermissionEval do
describe '#validate_prj_permissions/2' do describe '#validate_prj_permissions/2' do
it 'returns a boolean' do it 'returns a boolean' do
value = subject.send(:validate_prj_permissions, user, project) value = subject.__send__(:validate_prj_permissions, user, team, project)
expect(value).to be_in([true, false]) expect(value).to be_in([true, false])
end end
it 'returns false on wrong team' do
value = subject.__send__(:validate_prj_permissions, user, another_team, project)
expect(value).to be false
end
it 'returns true on the same team' do
value = subject.__send__(:validate_prj_permissions, user, team, project)
expect(value).to be true
end
end end
describe '#validate_exp_permissions/2' do describe '#validate_exp_permissions/2' do
it 'returns a boolean' do it 'returns a boolean' do
value = subject.send(:validate_exp_permissions, user, experiment) value = subject.__send__(:validate_exp_permissions, user, team, experiment)
expect(value).to be_in([true, false]) expect(value).to be_in([true, false])
end end
it 'returns false on wrong team' do
value = subject.__send__(:validate_exp_permissions, user, another_team, experiment)
expect(value).to be false
end
it 'returns true on the same team' do
value = subject.__send__(:validate_exp_permissions, user, team, experiment)
expect(value).to be true
end
end end
describe '#validate_tsk_permissions/2' do describe '#validate_tsk_permissions/2' do
it 'returns a boolean' do it 'returns a boolean' do
value = subject.send(:validate_tsk_permissions, user, task) value = subject.__send__(:validate_tsk_permissions, user, team, task)
expect(value).to be_in([true, false]) expect(value).to be_in([true, false])
end end
it 'returns false on wrong team' do
value = subject.__send__(:validate_tsk_permissions, user, another_team, task)
expect(value).to be false
end
it 'returns true on the same team' do
value = subject.__send__(:validate_tsk_permissions, user, team, task)
expect(value).to be true
end
end end
describe '#validate_rep_item_permissions/2' do describe '#validate_rep_item_permissions/2' do
it 'returns a boolean' do it 'returns a boolean' do
value = subject.send(:validate_rep_item_permissions, user, repository_item) value = subject.__send__(:validate_rep_item_permissions, user, team, repository_item)
expect(value).to be_in([true, false]) expect(value).to be_in([true, false])
end end
it 'returns false on wrong team' do
value = subject.__send__(:validate_rep_item_permissions, user, another_team, repository_item)
expect(value).to be false
end
it 'returns true on the same team' do
value = subject.__send__(:validate_rep_item_permissions, user, team, repository_item)
expect(value).to be true
end
end end
end end

View file

@ -11,7 +11,7 @@ describe SmartAnnotations::TagToHtml do
let(:text) do let(:text) do
"My annotation of [#my project~prj~#{project.id.base62_encode}]" "My annotation of [#my project~prj~#{project.id.base62_encode}]"
end end
let(:subject) { described_class.new(user, text) } let(:subject) { described_class.new(user, team, text) }
describe 'Parsed text' do describe 'Parsed text' do
it 'returns a existing string with smart annotation' do it 'returns a existing string with smart annotation' do
expect(subject.html).to eq( expect(subject.html).to eq(