-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'tess/master' into feature/simple_calendar
- Loading branch information
Showing
29 changed files
with
460 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,32 @@ | ||
require 'net/http' | ||
|
||
class HttpUrlValidator < ActiveModel::EachValidator | ||
|
||
def self.blocked?(value) | ||
blocked = (TeSS::Config.blocked_domains || []).any? do |regex| | ||
(TeSS::Config.blocked_domains || []).any? do |regex| | ||
value =~ regex | ||
end | ||
end | ||
|
||
def self.accessible?(value) | ||
begin | ||
case Net::HTTP.get_response(URI.parse(value)) | ||
when Net::HTTPSuccess then true | ||
else false | ||
uri = URI.parse(value) rescue nil | ||
if uri && (uri.scheme == 'http' || uri.scheme == 'https') | ||
PrivateAddressCheck.only_public_connections do | ||
res = HTTParty.get(value, { timeout: Rails.env.test? ? 1 : 5 }) | ||
res.code == 200 | ||
end | ||
end | ||
rescue | ||
rescue PrivateAddressCheck::PrivateConnectionAttemptedError, Net::OpenTimeout, SocketError, Errno::ECONNREFUSED, | ||
Errno::EHOSTUNREACH | ||
false | ||
end | ||
|
||
end | ||
|
||
def validate_each(record, attribute, value) | ||
if value.present? | ||
record.errors.add(attribute, "is blocked") if self.class.blocked?(value) | ||
record.errors.add(attribute, "is not accessible") unless self.class.accessible?(value) | ||
if self.class.blocked?(value) | ||
record.errors.add(attribute, 'is blocked') | ||
elsif !self.class.accessible?(value) | ||
record.errors.add(attribute, 'is not accessible') | ||
end | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class OrcidValidator < ActiveModel::EachValidator | ||
ORCID_PREFIX = 'https://orcid.org/'.freeze | ||
ORCID_DOMAIN_REGEX = /http(s)?\:\/\/orcid.org\//.freeze | ||
ORCID_ID_REGEX = /\A[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9,X]{4}\Z/.freeze | ||
|
||
def validate_each(record, attribute, value) | ||
return if value.nil? || valid_orcid_id?(value.sub(ORCID_DOMAIN_REGEX, '')) | ||
record.errors.add(attribute, options[:message] || "isn't a valid ORCID identifier") | ||
end | ||
|
||
private | ||
|
||
# checks the structure of the id, and whether is conforms to ISO/IEC 7064:2003 | ||
def valid_orcid_id?(id) | ||
if id =~ ORCID_ID_REGEX | ||
id = id.delete('-') | ||
id[15] == orcid_checksum(id) | ||
else | ||
false | ||
end | ||
end | ||
|
||
# calculating the checksum according to ISO/IEC 7064:2003, MOD 11-2 ; | ||
# see - https://support.orcid.org/hc/en-us/articles/360006897674-Structure-of-the-ORCID-Identifier | ||
def orcid_checksum(id) | ||
total = id.chars.first(15).inject(0) { |sum, value| (sum + value.to_i) * 2 } | ||
remainder = total % 11 | ||
result = (12 - remainder) % 11 | ||
result == 10 ? 'X' : result.to_s | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.