Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register role by user and include it in the center authorization #24

Merged
merged 11 commits into from
Dec 5, 2024
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: 4 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres
DATABASE_HOST: localhost
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}

steps:
- uses: actions/checkout@v2
Expand All @@ -43,15 +44,11 @@ jobs:
run: bundle exec rake test_app

- name: Run RSpec
run: SIMPLECOV=1 CODECOV=1 bundle exec rspec
run: CI=1 bundle exec rspec

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- uses: actions/upload-artifact@v2-preview
- uses: actions/upload-artifact@v3
if: always()
with:
name: screenshots
path: ./spec/decidim_dummy_app/tmp/screenshots
if-no-files-found: ignore
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16.9.1
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ group :development do
end

group :test do
gem "codecov", require: false
gem "coveralls_reborn", require: false
end
18 changes: 14 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
decidim-centers (0.1.1)
decidim-centers (0.2.0)
decidim-core (>= 0.27.0, < 0.28)
deface (~> 1.9)

Expand Down Expand Up @@ -97,6 +97,7 @@ GEM
html_tokenizer (~> 0.0.6)
parser (>= 2.4)
smart_properties
bigdecimal (3.1.8)
bindex (0.8.1)
bootsnap (1.17.0)
msgpack (~> 1.2)
Expand Down Expand Up @@ -135,8 +136,6 @@ GEM
chef-utils (18.3.0)
concurrent-ruby
childprocess (4.1.0)
codecov (0.6.0)
simplecov (>= 0.15, < 0.22)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
coffee-rails (5.0.0)
Expand All @@ -148,6 +147,11 @@ GEM
coffee-script-source (1.12.2)
commonmarker (0.23.10)
concurrent-ruby (1.2.2)
coveralls_reborn (0.25.0)
simplecov (>= 0.18.1, < 0.22.0)
term-ansicolor (~> 1.6)
thor (>= 0.20.3, < 2.0)
tins (~> 1.16)
crack (0.4.5)
rexml
crass (1.0.6)
Expand Down Expand Up @@ -745,13 +749,19 @@ GEM
activesupport (>= 5.2)
sprockets (>= 3.0.0)
ssrf_filter (1.1.2)
sync (0.5.0)
temple (0.10.3)
term-ansicolor (1.11.2)
tins (~> 1.0)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
thor (1.3.0)
thread_safe (0.3.6)
tilt (2.3.0)
timeout (0.4.1)
tins (1.37.0)
bigdecimal
sync
tomlrb (2.0.3)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -811,7 +821,7 @@ PLATFORMS
DEPENDENCIES
bootsnap (~> 1.4)
byebug (~> 11.0)
codecov
coveralls_reborn
decidim (= 0.27.4)
decidim-centers!
decidim-dev (= 0.27.4)
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
[![[CI] Lint](https://github.com/Platoniq/decidim-module-centers/actions/workflows/lint.yml/badge.svg)](https://github.com/Platoniq/decidim-module-centers/actions/workflows/lint.yml)
[![[CI] Test](https://github.com/Platoniq/decidim-module-centers/actions/workflows/test.yml/badge.svg)](https://github.com/Platoniq/decidim-module-centers/actions/workflows/test.yml)
[![Maintainability](https://api.codeclimate.com/v1/badges/6b1b656b229f9731a64b/maintainability)](https://codeclimate.com/github/Platoniq/decidim-module-centers/maintainability)
[![codecov](https://codecov.io/gh/Platoniq/decidim-module-centers/branch/main/graph/badge.svg)](https://codecov.io/gh/Platoniq/decidim-module-centers)
[![Coverage Status](https://coveralls.io/repos/github/Platoniq/decidim-module-centers/badge.svg?branch=main)](https://coveralls.io/github/Platoniq/decidim-module-centers?branch=main)

Manage your centers and scopes so the users can be authorized over them. As an admin you will be able
to create centers and scopes (we use the model Decidim currently provides).
Manage your centers, roles and scopes so the users can be authorized over them. As an admin you will be able
to create centers, roles and scopes (we use the model Decidim currently provides).

When a user signs up in the platform two new fields will appear in the registration form.

- **Center**: Where the user works. For example: "University of Granada"
- **Scope**: The work scope in which the user works. For example: "Computer Science"
- **Role**: The role of the user in the center. For example: "Professor"

![Registration form](examples/registration.png)

Expand All @@ -22,8 +23,8 @@ When they create the account or update their values, a `center` authorization wi
with the value of the center and scope the user has selected.

As an admin you will be able to configure the permissions of a component restricting the access to
specific centers and scopes. When you select multiple centers or scopes they work as "or". When you specify
both the center and the scope it will work as an "and" between them.
specific centers, roles and scopes. When you select multiple centers, roles or scopes they work as "or". When you specify
the center, the role and the scope it will work as an "and" between them.

![Permissions in the admin page](examples/permissions.png)

Expand Down Expand Up @@ -53,14 +54,16 @@ Depending on your Decidim version, choose the corresponding version to ensure co
| Version | Compatible decidim versions |
|---------|-----------------------------|
| 0.1.x | v0.27.x |
| 0.2.x | v0.27.x |

## Configuration

You can customize your installation using the environment variables below:

| ENV | Description | Default | Example |
|--------------------------------------|-----------------------------------------------------------|---------|--------------|
| DECIDIM_CENTERS_SCOPES_ENABLED | Use scopes to categorize users too along with the centers | true | false |
| ENV | Description | Default | Example |
|--------------------------------|-----------------------------------------------------------|---------|--------------|
| DECIDIM_CENTERS_SCOPES_ENABLED | Use scopes to categorize users too along with the centers | true | false |
| DECIDIM_CENTERS_ROLES_ENABLED | Use roles to categorize users too along with the centers | true | false |

> **IMPORTANT**: Remember to activate the verification method `center` in the
> Decidim `/system` admin page for your organization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def publish_center_update_event
"decidim.centers.user.updated",
user_id: @user.id,
center_id: @form.center_id,
role_id: @form.role_id,
scope_id: @form.scope_id
)
end
Expand Down
50 changes: 50 additions & 0 deletions app/commands/decidim/centers/admin/create_role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module Decidim
module Centers
module Admin
# This command is executed when the user creates a Role from the admin
# panel.
class CreateRole < Decidim::Command
# Initializes a CreateRole Command.
#
# form - The form from which to get the data.
# current_user - The user who performs the action.
def initialize(form, current_user)
@form = form
@current_user = current_user
end

# Creates the role if valid.
#
# Broadcasts :ok if successful, :invalid otherwise.
def call
return broadcast(:invalid) if form.invalid?

transaction do
create_role!
end

broadcast(:ok, @role)
end

private

attr_reader :form, :current_user

def create_role!
attributes = {
organization: form.current_organization,
title: form.title
}

@role = Decidim.traceability.create!(
Role,
current_user,
attributes
)
end
end
end
end
end
47 changes: 47 additions & 0 deletions app/commands/decidim/centers/admin/destroy_role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

module Decidim
module Centers
module Admin
# This command is executed when the user destroys a Role from the admin
# panel.
class DestroyRole < Decidim::Command
# Initializes a DestroyRole Command.
#
# role - The current instance of the role to be destroyed.
# current_user - The user who performs the action.
def initialize(role, current_user)
@role = role
@current_user = current_user
end

# Destroys the role if valid.
#
# Broadcasts :ok if successful, :invalid otherwise.
def call
transaction do
destroy_role!
end

broadcast(:ok, role)
end

private

attr_reader :role, :current_user

def destroy_role!
attributes = {
deleted_at: Time.current
}

Decidim.traceability.update!(
role,
current_user,
attributes
)
end
end
end
end
end
47 changes: 47 additions & 0 deletions app/commands/decidim/centers/admin/update_role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

module Decidim
module Centers
module Admin
# This command is executed when the user changes a Role from the admin
# panel.
class UpdateRole < Decidim::Command
# Initializes a UpdateRole Command.
#
# form - The form from which to get the data.
# role - The current instance of the role to be updated.
# current_user - The user who performs the action.
def initialize(form, role, current_user)
@form = form
@role = role
@current_user = current_user
end

# Updates the role if valid.
#
# Broadcasts :ok if successful, :invalid otherwise.
def call
return broadcast(:invalid) if form.invalid?

transaction do
update_role!
end

broadcast(:ok, role)
end

private

attr_reader :form, :role, :current_user

def update_role!
Decidim.traceability.update!(
role,
current_user,
title: form.title
)
end
end
end
end
end
44 changes: 44 additions & 0 deletions app/commands/decidim/centers/create_or_update_role_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

module Decidim
module Centers
# This command is executed when a new relationship between user
# and role is created
class CreateOrUpdateRoleUser < Decidim::Command
# Initializes a CreateOrUpdateRoleUser Command.
#
# role - The role to be related with the user.
# user - The user to be related with the role.
def initialize(role, user)
@role = role
@user = user
end

# Creates or update the role user if valid.
#
# Broadcasts :ok if successful, :invalid otherwise.
def call
transaction do
delete_existing_role_user!
create_role_user!
rescue ActiveRecord::RecordInvalid
broadcast(:invalid)
end

broadcast(:ok, @role_user)
end

private

attr_reader :role, :user

def delete_existing_role_user!
RoleUser.where(user: user).destroy_all
end

def create_role_user!
@role_user = RoleUser.create!(role: role, user: user)
end
end
end
end
Loading