Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions app/controllers/roles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ def create
authorize @role
access_level = params[:role][:access_level].to_i
set_access_level(access_level)
message = ''
if params[:user].present?
if @role.plan.owner.present? && @role.plan.owner.email == params[:user]
flash[:notice] = _('Cannot share plan with %{email} since that email matches with the owner of the plan.') % {email: params[:user]}
else
if Role.find_by(plan: @role.plan, user: User.find_by(email: params[:user])) # role already exists
user = User.where_case_insensitive('email',params[:user]).first
if Role.find_by(plan: @role.plan, user: user) # role already exists
flash[:notice] = _('Plan is already shared with %{email}.') % {email: params[:user]}
else
message = _('Plan shared with %{email}.') % {email: params[:user]}
user = User.find_by(email: params[:user])
else
if user.nil?
registered = false
User.invite!(email: params[:user])
message = _('Invitation to %{email} issued successfully.') % {email: params[:user]}
message = _('Invitation to %{email} issued successfully. \n') % {email: params[:user]}
user = User.find_by(email: params[:user])
end
message += _('Plan shared with %{email}.') % {email: user.email}
@role.user = user
if @role.save
if registered then UserMailer.sharing_notification(@role, current_user).deliver_now end
Expand Down
16 changes: 8 additions & 8 deletions app/models/settings/template.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Settings
class Template < RailsSettings::SettingObject

#attr_accessible :var, :target, :target_id, :target_type

VALID_FONT_FACES = [
'Arial, Helvetica, Sans-Serif',
'"Times New Roman", Times, Serif'
'"Times New Roman", Times, Serif',
'Arial, Helvetica, Sans-Serif'
]

VALID_FONT_SIZE_RANGE = (8..14)
Expand All @@ -17,13 +17,13 @@ class Template < RailsSettings::SettingObject
DEFAULT_SETTINGS = {
formatting: {
margin: { # in millimeters
top: 20,
bottom: 20,
left: 20,
right: 20
top: 10,
bottom: 10,
left: 10,
right: 10
},
font_face: VALID_FONT_FACES.first,
font_size: 12 # pt
font_size: 10 # pt
},
max_pages: 3,
fields: {
Expand Down
45 changes: 26 additions & 19 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :invitable, :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable, :omniauthable,
devise :invitable, :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable, :omniauthable,
:omniauth_providers => [:shibboleth, :orcid]

##
Expand All @@ -26,27 +26,27 @@ def filter(query)
q = "%#{query}%"
conditions = t[:title].matches(q)
columns = %i(
grant_number identifier description principal_investigator data_contact
grant_number identifier description principal_investigator data_contact
)
columns = ['grant_number', 'identifier', 'description', 'principal_investigator', 'data_contact']
columns.each {|col| conditions = conditions.or(t[col].matches(q)) }
self.where(conditions)
end
end

has_many :user_identifiers
has_many :identifier_schemes, through: :user_identifiers

##
# Possibly needed for active_admin
# -relies on protected_attributes gem as syntax depricated in rails 4.2
#accepts_nested_attributes_for :roles
#attr_accessible :password_confirmation, :encrypted_password, :remember_me,
# :id, :email, :firstname, :last_login,:login_count, :orcid_id,
# :password, :shibboleth_id, :user_status_id, :surname,
# :user_type_id, :org_id, :skip_invitation, :other_organisation,
#attr_accessible :password_confirmation, :encrypted_password, :remember_me,
# :id, :email, :firstname, :last_login,:login_count, :orcid_id,
# :password, :shibboleth_id, :user_status_id, :surname,
# :user_type_id, :org_id, :skip_invitation, :other_organisation,
# :accept_terms, :role_ids, :dmponline3, :api_token,
# :organisation, :language, :language_id, :org, :perms,
# :organisation, :language, :language_id, :org, :perms,
# :confirmed_at, :org_id

validates :email, email: true, allow_nil: true, uniqueness: {message: _("must be unique")}
Expand All @@ -62,13 +62,13 @@ def filter(query)
# What do they do? do they do it efficiently, and do we need them?

# Determines the locale set for the user or the organisation he/she belongs
# @return String or nil
# @return String or nil
def get_locale
if !self.language.nil?
return self.language.abbreviation
elsif !self.org.nil?
return self.org.get_locale
else
else
return nil
end
end
Expand Down Expand Up @@ -126,7 +126,7 @@ def org_id=(new_org_id)
def organisation=(new_org)
org_id = new_org.id unless new_org.nil?
end

##
# checks if the user is a super admin
# if the user has any privelege which requires them to see the super admin page
Expand All @@ -144,7 +144,7 @@ def can_super_admin?
#
# @return [Boolean] true if the user is an organisation admin
def can_org_admin?
return self.can_grant_permissions? || self.can_modify_guidance? ||
return self.can_grant_permissions? || self.can_modify_guidance? ||
self.can_modify_templates? || self.can_modify_org_details?
end

Expand Down Expand Up @@ -223,7 +223,7 @@ def org_type
return org_type
end
=end

##
# removes the api_token from the user
# modifies the user model
Expand Down Expand Up @@ -254,11 +254,11 @@ def keep_or_generate_token!
# --------------------------------------------------------------
def self.from_omniauth(auth)
scheme = IdentifierScheme.find_by(name: auth.provider.downcase)

if scheme.nil?
throw Exception.new('Unknown OAuth provider: ' + auth.provider)
else
joins(:user_identifiers).where('user_identifiers.identifier': auth.uid,
joins(:user_identifiers).where('user_identifiers.identifier': auth.uid,
'user_identifiers.identifier_scheme_id': scheme.id).first
end
end
Expand All @@ -269,7 +269,14 @@ def self.from_omniauth(auth)
def deliver_invitation(options = {})
super(options.merge(subject: _('A Data Management Plan in %{application_name} has been shared with you') % {application_name: Rails.configuration.branding[:application][:name]}))
end

##
# Case insensitive search over User model
# @param field [string] The name of the field being queried
# @param val [string] The string to search for, case insensitive
# @return [ActiveRecord::Relation] The result of the search
def self.where_case_insensitive(field, val)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Rails 5 we can move this method to the new ApplicationRecord.rb to make it available to all of the models

User.where("lower(#{field}) = ?", val.downcase)
end

# TODO: Remove this, its never called.
# this generates a reset password link for a given user
Expand All @@ -278,12 +285,12 @@ def deliver_invitation(options = {})
=begin
def reset_password_link
raw, enc = Devise.token_generator.generate(self.class, :reset_password_token)
self.reset_password_token = enc
self.reset_password_token = enc
self.reset_password_sent_at = Time.now.utc
save(validate: false)

edit_user_password_path + '?reset_password_token=' + raw
end
=end

end
40 changes: 20 additions & 20 deletions test/functional/roles_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class RolesControllerTest < ActionDispatch::IntegrationTest
setup do
scaffold_plan
scaffold_org_admin(@plan.template.org)

# This should NOT be unnecessary! Owner should have full access
@plan.roles << Role.create(user: @user, plan: @plan, access: 15)

end

# TODO: Cleanup routes for this one. The controller currently only responds to create, update, destroy
Expand All @@ -21,19 +21,19 @@ class RolesControllerTest < ActionDispatch::IntegrationTest
# role PATCH /roles/:id roles#update
# PUT /roles/:id roles#update
# DELETE /roles/:id roles#destroy

# POST /roles (roles_path)
# ----------------------------------------------------------
test "create a new role" do

params = {plan_id: @plan.id, access_level: 4}

# Should redirect user to the root path if they are not logged in!
post roles_path, {role: params}
assert_unauthorized_redirect_to_root_path

sign_in @user

# Known user
@invitee = User.where.not(id: [@plan.owner.id, @user.id]).first
post roles_path, {user: @invitee.email, role: params}
Expand All @@ -50,15 +50,15 @@ class RolesControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to share_plan_path(@plan)
assert_equal @invitee.id, Role.last.user_id, "expected no record to have been created!"
assert assigns(:role)

# Unknown user
post roles_path, {user: 'unknown_user@org.org', role: params}
assert_equal _('Invitation to unknown_user@org.org issued successfully.'), flash[:notice]
assert_equal _('Invitation to unknown_user@org.org issued successfully. \nPlan shared with unknown_user@org.org.'), flash[:notice]
assert_response :redirect
assert_redirected_to share_plan_path(@plan)
assert_equal User.find_by(email:'unknown_user@org.org').id, Role.last.user_id, "expected the record to have been created!"
assert assigns(:role)

# Invite owner
@invitee = User.find_by(id: @plan.owner.id)
post roles_path, {user: @invitee.email, role: params}
Expand All @@ -67,26 +67,26 @@ class RolesControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to share_plan_path(@plan)
assert_not_equal @invitee.id, Role.last.user_id, "expected no record to have been created!"
assert assigns(:role)

# Missing email
post roles_path, {role: {plan_id: @plan.id, access_level: 4}}
assert_equal _('Please enter an email address'), flash[:notice]
assert_response :redirect
assert_redirected_to share_plan_path(@plan)
assert assigns(:role)
end
end

# PUT /role/:id (role_path)
# ----------------------------------------------------------
test "update the role" do
@invitee = User.last
role = Role.create(user: @invitee, plan: @plan, access: 1)
params = {access_level: 2}

# Should redirect user to the root path if they are not logged in!
put role_path(role), {role: params}
assert_unauthorized_redirect_to_root_path

sign_in @user

# Valid save
Expand All @@ -96,7 +96,7 @@ class RolesControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to share_plan_path(@plan)
assert assigns(:role)
assert_equal 13, role.reload.access, "expected the record to have been updated"

# TODO: Role should require a user, plan and an access level :/
# Invalid save
# put role_path(role), {role: {user: nil}}
Expand All @@ -105,26 +105,26 @@ class RolesControllerTest < ActionDispatch::IntegrationTest
# assert_redirected_to share_plan_path(@plan)
# assert assigns(:role)
end

# DELETE /role/:id (role_path)
# ----------------------------------------------------------
test "delete the section" do
@invitee = User.last
role = Role.create(user: @invitee, plan: @plan, access: 1)

# Should redirect user to the root path if they are not logged in!
delete role_path(role)
assert_unauthorized_redirect_to_root_path

sign_in @user

delete role_path(role)
assert_equal _('Access removed'), flash[:notice]
assert_response :redirect
assert_redirected_to share_plan_path(@plan)
assert_raise ActiveRecord::RecordNotFound do
assert_raise ActiveRecord::RecordNotFound do
Role.find(role.id).nil?
end
end

end