Skip to content

Commit

Permalink
[#58143] Add the new role and apply defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
dombesz committed Oct 2, 2024
1 parent df3cd50 commit 58da028
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 26 deletions.
11 changes: 11 additions & 0 deletions app/models/global_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ def self.givable
super
.where(type: "GlobalRole")
end

def self.standard
standard_global_role = where(builtin: BUILTIN_STANDARD_GLOBAL).first
if standard_global_role.nil?
standard_global_role = create(name: "Standard global role", position: 0) do |role|
role.builtin = BUILTIN_STANDARD_GLOBAL
end
raise "Unable to create the standard global role." if standard_global_role.new_record?
end
standard_global_role
end
end
4 changes: 3 additions & 1 deletion app/models/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Role < ApplicationRecord
BUILTIN_WORK_PACKAGE_EDITOR = 5
BUILTIN_PROJECT_QUERY_VIEW = 6
BUILTIN_PROJECT_QUERY_EDIT = 7
BUILTIN_STANDARD_GLOBAL = 8

HIDDEN_ROLE_TYPES = [
"WorkPackageRole",
Expand Down Expand Up @@ -93,7 +94,8 @@ def self.givable
Role::BUILTIN_WORK_PACKAGE_COMMENTER,
Role::BUILTIN_WORK_PACKAGE_EDITOR,
Role::BUILTIN_PROJECT_QUERY_VIEW,
Role::BUILTIN_PROJECT_QUERY_EDIT
Role::BUILTIN_PROJECT_QUERY_EDIT,
Role::BUILTIN_STANDARD_GLOBAL
]
)
.order(Arel.sql("position"))
Expand Down
1 change: 1 addition & 0 deletions app/seeders/basic_data/base_role_seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def builtin(value)
case value
when :non_member then Role::BUILTIN_NON_MEMBER
when :anonymous then Role::BUILTIN_ANONYMOUS
when :standard_global then Role::BUILTIN_STANDARD_GLOBAL
when :work_package_editor then Role::BUILTIN_WORK_PACKAGE_EDITOR
when :work_package_commenter then Role::BUILTIN_WORK_PACKAGE_COMMENTER
when :work_package_viewer then Role::BUILTIN_WORK_PACKAGE_VIEWER
Expand Down
6 changes: 6 additions & 0 deletions app/seeders/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,9 @@ global_roles:
- :create_user
- :manage_user
- :manage_placeholder_user
- reference: :default_role_standard_global
t_name: Standard global role
position: 7
builtin: :standard_global
permissions:
- :view_user_email
5 changes: 5 additions & 0 deletions config/initializers/permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
require: :loggedin,
contract_actions: { placeholder_users: %i[create read update] }

map.permission :view_user_email,
{},
permissible_on: :global,
require: :loggedin

map.permission :view_project,
{ projects: [:show] },
permissible_on: :project,
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3045,6 +3045,7 @@ en:
permission_view_shared_work_packages: "View work package shares"
permission_view_time_entries: "View spent time"
permission_view_timelines: "View timelines"
permission_view_user_email: "View users' mail addresses"
permission_view_wiki_edits: "View wiki history"
permission_view_wiki_pages: "View wiki"
permission_work_package_assigned: "Become assignee/responsible"
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20241001205821_add_standard_global_role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AddStandardGlobalRole < ActiveRecord::Migration[7.1]
def change
standard_global_role ||= GlobalRole.find_or_initialize_by(builtin: Role::BUILTIN_STANDARD_GLOBAL)

standard_global_role.update!(
name: I18n.t("seeds.common.global_roles.item_1.name", default: "Standard global role"),
permissions: %i[
view_user_email
]
)
end
end
6 changes: 6 additions & 0 deletions spec/factories/global_role_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@
FactoryBot.define do
factory :global_role do
sequence(:name) { |n| "Global Role #{n}" }

factory :standard_global_role do
name { "Standard global role" }
builtin { Role::BUILTIN_STANDARD_GLOBAL }
initialize_with { GlobalRole.where(builtin: Role::BUILTIN_STANDARD_GLOBAL).first_or_initialize }
end
end
end
68 changes: 46 additions & 22 deletions spec/models/global_role_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,28 @@
require "spec_helper"

RSpec.describe GlobalRole do
before { GlobalRole.create name: "globalrole", permissions: ["permissions"] }
let!(:global_role) { create(:global_role, name: "globalrole", permissions: ["permissions"]) }

it { is_expected.to validate_presence_of :name }
it { is_expected.to validate_uniqueness_of :name }
it { is_expected.to validate_length_of(:name).is_at_most(256) }

describe "attributes" do
before { @role = GlobalRole.new }

subject { @role }
subject(:role) { described_class.new }

it { is_expected.to respond_to :name }
it { is_expected.to respond_to :permissions }
it { is_expected.to respond_to :position }
end

describe "instance methods" do
before do
@role = GlobalRole.new
end

describe "WITH no attributes set" do
before do
@role = GlobalRole.new
end
let(:role) { described_class.new }

subject { role }

describe "#permissions" do
subject { @role.permissions }
subject { role.permissions }

it { is_expected.to be_an_instance_of(Array) }

Expand All @@ -66,38 +60,68 @@
end

describe "#has_permission?" do
it { expect(@role.has_permission?(:perm)).to be_falsey }
it { is_expected.not_to have_permission(:perm) }
end

describe "#allowed_to?" do
describe "WITH requested permission" do
it { expect(@role.allowed_to?(:perm1)).to be_falsey }
it { is_expected.not_to be_allowed_to(:perm1) }
end
end
end

describe "WITH set permissions" do
before { @role = GlobalRole.new permissions: %i[perm1 perm2 perm3] }
subject(:role) { described_class.new permissions: %i[perm1 perm2 perm3] }

describe "#has_permission?" do
it { expect(@role.has_permission?(:perm1)).to be_truthy }
it { expect(@role.has_permission?("perm1")).to be_truthy }
it { expect(@role.has_permission?(:perm5)).to be_falsey }
it { is_expected.to have_permission(:perm1) }
it { is_expected.to have_permission("perm1") }
it { is_expected.not_to have_permission(:perm5) }
end

describe "#allowed_to?" do
describe "WITH requested permission" do
it { expect(@role.allowed_to?(:perm1)).to be_truthy }
it { expect(@role.allowed_to?(:perm5)).to be_falsey }
it { is_expected.to be_allowed_to(:perm1) }
it { is_expected.not_to be_allowed_to(:perm5) }
end
end
end

describe "WITH set name" do
before { @role = GlobalRole.new name: "name" }
let(:role) { described_class.new name: "name" }

describe "#to_s" do
it { expect(@role.to_s).to eql("name") }
it { expect(role.to_s).to eql("name") }
end
end
end

describe ".givable" do
let!(:builtin_role) { create(:standard_global_role) }

it { expect(described_class.givable).to contain_exactly global_role }
end

describe ".standard" do
subject { described_class.standard }

it "has the constant's builtin value" do
expect(subject.builtin)
.to eql(Role::BUILTIN_STANDARD_GLOBAL)
end

it "is builtin" do
expect(subject)
.to be_builtin
end

context "with a missing standard role" do
before do
described_class.where(builtin: Role::BUILTIN_STANDARD_GLOBAL).delete_all
end

it "creates a new standard role" do
expect { subject }.to change(described_class, :count).by(1)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/seeders/root_seeder_standard_edition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
expect(Query.count).to eq 26
expect(ProjectRole.count).to eq 5
expect(WorkPackageRole.count).to eq 3
expect(GlobalRole.count).to eq 1
expect(GlobalRole.count).to eq 2
expect(Grids::Overview.count).to eq 2
expect(Version.count).to eq 4
expect(VersionSetting.count).to eq 4
Expand Down Expand Up @@ -126,7 +126,7 @@

include_examples "it creates records", model: Color, expected_count: 144
include_examples "it creates records", model: DocumentCategory, expected_count: 3
include_examples "it creates records", model: GlobalRole, expected_count: 1
include_examples "it creates records", model: GlobalRole, expected_count: 2
include_examples "it creates records", model: WorkPackageRole, expected_count: 3
include_examples "it creates records", model: ProjectRole, expected_count: 5
include_examples "it creates records", model: ProjectQueryRole, expected_count: 2
Expand Down Expand Up @@ -166,7 +166,7 @@
expect(Query.count).to eq 26
expect(ProjectRole.count).to eq 5
expect(WorkPackageRole.count).to eq 3
expect(GlobalRole.count).to eq 1
expect(GlobalRole.count).to eq 2
expect(Grids::Overview.count).to eq 2
expect(Version.count).to eq 4
expect(VersionSetting.count).to eq 4
Expand Down

0 comments on commit 58da028

Please sign in to comment.