From 3d4f9f2c280f9d85722951fa5850ca00a7e80aac Mon Sep 17 00:00:00 2001 From: hellekin Date: Thu, 29 Oct 2020 22:40:35 +0100 Subject: REUSE: Add AGPL-3.0-or-later to app/* --- app/controllers/agents_controller.rb | 91 +++++++++++++++++++++++++++++++ app/controllers/application_controller.rb | 6 +- app/controllers/categories_controller.rb | 23 ++++++++ app/controllers/my/agent_controller.rb | 4 ++ app/controllers/resources_controller.rb | 4 ++ app/controllers/users_controller.rb | 4 ++ app/controllers/welcome_controller.rb | 4 ++ app/helpers/agents_helper.rb | 4 ++ app/helpers/application_helper.rb | 4 ++ app/helpers/categories_helper.rb | 6 ++ app/helpers/resources_helper.rb | 4 ++ app/helpers/taxonomies_helper.rb | 23 ++++++++ app/helpers/users_helper.rb | 4 ++ app/helpers/welcome_helper.rb | 4 ++ app/jobs/agency_watcher_job.rb | 4 ++ app/jobs/application_job.rb | 4 ++ app/lib/incommon.rb | 6 ++ app/lib/sso.rb | 4 ++ app/lib/sso/from_discourse.rb | 4 ++ app/mailers/application_mailer.rb | 4 ++ app/models/agency.rb | 4 ++ app/models/agent.rb | 4 ++ app/models/application_record.rb | 4 ++ app/models/category.rb | 4 ++ app/models/classification.rb | 4 ++ app/models/resource.rb | 4 ++ app/models/section.rb | 4 ++ app/models/taxonomy.rb | 4 ++ app/models/user.rb | 4 ++ app/validators/email_validator.rb | 33 +++++++++++ app/validators/url_validator.rb | 33 +++++++++++ 31 files changed, 311 insertions(+), 2 deletions(-) create mode 100644 app/controllers/agents_controller.rb create mode 100644 app/controllers/categories_controller.rb create mode 100644 app/helpers/categories_helper.rb create mode 100644 app/helpers/taxonomies_helper.rb create mode 100644 app/lib/incommon.rb create mode 100644 app/validators/email_validator.rb create mode 100644 app/validators/url_validator.rb (limited to 'app') diff --git a/app/controllers/agents_controller.rb b/app/controllers/agents_controller.rb new file mode 100644 index 0000000..105e443 --- /dev/null +++ b/app/controllers/agents_controller.rb @@ -0,0 +1,91 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +class AgentsController < ApplicationController + before_action :set_agent, only: [:new, :show, :edit, :update, :delete, :destroy] + + # GET /agents + def index + @agents = Agent.order(:name).page params[:page] + end + + # GET /agents/new + def new + end + + # POST /agents + def create + flash.now[:alert] = 'Agents are created from actual groups for now...' + render :new + end + + # GET /agents/:id + def show + end + + # GET /agents/:id/edit + def edit + flash.now[:notice] = 'Please ask a maintainer to edit this resource!' unless current_user_maintainer? + end + + # PATCH /agents/:id + def update + # Check list: + # 1. Compare records for changes + # 2. Validate each change + # 3. Moderate queue or save + + return 403 unless current_user_maintainer? + + respond_to do |format| + if @agent.update(agent_params) + format.html { redirect_to @agent, notice: 'Merci de votre contribution !' } + format.json { render :show, status: :ok, location: @agent } + else + format.html { render :edit } + format.json { render json: @agent.errors, status: :unprocessable_entity } + end + end + end + + # GET /agents/:id/delete + def delete + flash.now[:notice] = 'Please ask a maintainer to delete your Agent!' unless current_user_maintainer? + end + + # DELETE /agents/:id + def destroy + return 403 # Yeah, right? + # Check list + # 1. User belongs to Agent and is :maintainer? + if !(current_user_maintainer? && current_user_leader?) + msg = 'You must be a maintainer and a leader to delete your Agent!' + respond_to do |format| + format.html { redirect_to :show, notice: msg } + format.json { render json: '{}', status: :forbidden, message: msg } + end + else + @agent.destroy + respond_to do |format| + format.html { redirect_to :index, notice: 'OK, resource Agent successfully removed.' } + format.json { head :no_content } + end + end + end + + private + + def agent_params + params + .require(:agent) + .permit(:uuid, + :name, + :summary, + :description) + end + + def set_agent + @agent = Agent.find_by(uuid: params[:id]) || Agent.new + end +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 14f50bf..5299f54 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,8 @@ -class ApplicationController < ActionController::Base - protect_from_forgery with: :null_session +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later +class ApplicationController < ActionController::Base require 'sso' before_action :current_user diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb new file mode 100644 index 0000000..40f1ff5 --- /dev/null +++ b/app/controllers/categories_controller.rb @@ -0,0 +1,23 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +class CategoriesController < ApplicationController + before_action :set_category, only: [:new, :show, :edit, :update, :delete, :destroy] + + def index + end + + def show + @category = Category.find(params[:id]) + end + + def edit + end + + private + + def set_category + @category = Category.find_by(id: params[:id]) || Category.new + end +end diff --git a/app/controllers/my/agent_controller.rb b/app/controllers/my/agent_controller.rb index ac761b7..6da2642 100644 --- a/app/controllers/my/agent_controller.rb +++ b/app/controllers/my/agent_controller.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class My::AgentController < ApplicationController # PATCH /my/current_agent def switch diff --git a/app/controllers/resources_controller.rb b/app/controllers/resources_controller.rb index 04d4e70..52d89ea 100644 --- a/app/controllers/resources_controller.rb +++ b/app/controllers/resources_controller.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class ResourcesController < ApplicationController before_action :set_resource, only: [:new, :show, :edit, :update, :delete, :destroy] diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2cfa2ab..9a3cf74 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class UsersController < ApplicationController # GET /my/users # If you're a leader, you will see a list of Agent members diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 0c41e5a..cafddd1 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class WelcomeController < ApplicationController # GET / def index diff --git a/app/helpers/agents_helper.rb b/app/helpers/agents_helper.rb index a02fb7a..6b2ff82 100644 --- a/app/helpers/agents_helper.rb +++ b/app/helpers/agents_helper.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + module AgentsHelper # Form to select an Agent def agent_selector_form diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2b6e109..4689613 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + module ApplicationHelper def current_agency current_user.agencies.where(agent: current_agent).first diff --git a/app/helpers/categories_helper.rb b/app/helpers/categories_helper.rb new file mode 100644 index 0000000..f6d7a86 --- /dev/null +++ b/app/helpers/categories_helper.rb @@ -0,0 +1,6 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +module CategoriesHelper +end diff --git a/app/helpers/resources_helper.rb b/app/helpers/resources_helper.rb index 157a8f5..a949311 100644 --- a/app/helpers/resources_helper.rb +++ b/app/helpers/resources_helper.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + module ResourcesHelper # Return a SELECT tag to choose a section in a given taxonomy def section_select(taxonomy, selected = nil) diff --git a/app/helpers/taxonomies_helper.rb b/app/helpers/taxonomies_helper.rb new file mode 100644 index 0000000..a584177 --- /dev/null +++ b/app/helpers/taxonomies_helper.rb @@ -0,0 +1,23 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +# coding: utf-8 +# frozen_string_literal: true + +module TaxonomiesHelper + def taxonomy_filter + @taxonomy ||= Taxonomy.first + html = [] + @taxonomy.categories.each do |cat| + list = [] + cat.sections.each do |sec| + list << tag.li(h("#{sec.rank}. #{sec.name}"), id: "section-#{sec.id}", data: { action: "taxonomy#section" }) + end + html << tag.li(h("#{cat.rank}. #{cat.name}") << tag.ol(list.join.html_safe), + id: "category-#{cat.id}", + data: { action: "taxonomy#category" }) + end + raw(tag.nav(tag.ol(html.join.html_safe), id: "taxonomy-#{@taxonomy.uuid}")) + end +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 2310a24..0adb3be 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -1,2 +1,6 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + module UsersHelper end diff --git a/app/helpers/welcome_helper.rb b/app/helpers/welcome_helper.rb index eeead45..8e575f1 100644 --- a/app/helpers/welcome_helper.rb +++ b/app/helpers/welcome_helper.rb @@ -1,2 +1,6 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + module WelcomeHelper end diff --git a/app/jobs/agency_watcher_job.rb b/app/jobs/agency_watcher_job.rb index 5cc0256..7a114d2 100644 --- a/app/jobs/agency_watcher_job.rb +++ b/app/jobs/agency_watcher_job.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + # frozen_string_literal: true # = AgencyWatcherJob diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb index d394c3d..9f811cb 100644 --- a/app/jobs/application_job.rb +++ b/app/jobs/application_job.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class ApplicationJob < ActiveJob::Base # Automatically retry jobs that encountered a deadlock # retry_on ActiveRecord::Deadlocked diff --git a/app/lib/incommon.rb b/app/lib/incommon.rb new file mode 100644 index 0000000..091f022 --- /dev/null +++ b/app/lib/incommon.rb @@ -0,0 +1,6 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +module INCOMMON +end diff --git a/app/lib/sso.rb b/app/lib/sso.rb index 0939a7c..088f436 100644 --- a/app/lib/sso.rb +++ b/app/lib/sso.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2018-2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + # frozen_string_literal: true # Perform Single Sign-On using Discourse diff --git a/app/lib/sso/from_discourse.rb b/app/lib/sso/from_discourse.rb index a5df192..94969c9 100644 --- a/app/lib/sso/from_discourse.rb +++ b/app/lib/sso/from_discourse.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2018-2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + # frozen_string_literal: true module SSO diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 286b223..1900454 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class ApplicationMailer < ActionMailer::Base default from: 'from@example.com' layout 'mailer' diff --git a/app/models/agency.rb b/app/models/agency.rb index 03e6e5a..7ffe489 100644 --- a/app/models/agency.rb +++ b/app/models/agency.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class Agency < ApplicationRecord include Bitfields diff --git a/app/models/agent.rb b/app/models/agent.rb index 6264ba3..28ba579 100644 --- a/app/models/agent.rb +++ b/app/models/agent.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class Agent < ApplicationRecord has_many :agencies has_many :members, through: :agencies, source: :user diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 10a4cba..81dc6c5 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class ApplicationRecord < ActiveRecord::Base self.abstract_class = true end diff --git a/app/models/category.rb b/app/models/category.rb index b8c49f1..6868bf7 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class Category < ApplicationRecord belongs_to :taxonomy has_many :sections, diff --git a/app/models/classification.rb b/app/models/classification.rb index c384aa7..0754054 100644 --- a/app/models/classification.rb +++ b/app/models/classification.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class Classification < ApplicationRecord belongs_to :resource belongs_to :section diff --git a/app/models/resource.rb b/app/models/resource.rb index 7d87e1a..8df106c 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + require_dependency 'phony_rails' class Resource < ApplicationRecord diff --git a/app/models/section.rb b/app/models/section.rb index 7d26882..16213c7 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class Section < ApplicationRecord belongs_to :category has_one :taxonomy, through: :category diff --git a/app/models/taxonomy.rb b/app/models/taxonomy.rb index 0810fc0..34f65f6 100644 --- a/app/models/taxonomy.rb +++ b/app/models/taxonomy.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class Taxonomy < ApplicationRecord # Universally Unique Identifier :uuid include UUIDParameter diff --git a/app/models/user.rb b/app/models/user.rb index f7e96a8..9b199ac 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + class User < ApplicationRecord has_many :agencies has_many :agents, through: :agencies, source: :agent diff --git a/app/validators/email_validator.rb b/app/validators/email_validator.rb new file mode 100644 index 0000000..4191907 --- /dev/null +++ b/app/validators/email_validator.rb @@ -0,0 +1,33 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +# frozen_string_literal: true + +# = Email Validator = +# +# In order to be considered valid, value must be: +# +# - a valid Email URI +# - parsable as an URI +# - have a hostname +# - have a valid public TLD +# +class EmailValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + unless valid_email_address?(value) + record.errors[attribute] << (options[:message] || 'is an invalid email address') + end + end + + private + + def valid_email_address?(value) + uri = URI.parse("mailto:#{value}") + uri.is_a?(URI::MailTo) && + uri.to == value && + IANA::TLD.valid?(value.split('@', 2).last.split('.').compact.last) + rescue URI::InvalidURIError, URI::InvalidComponentError + false + end +end diff --git a/app/validators/url_validator.rb b/app/validators/url_validator.rb new file mode 100644 index 0000000..5f790b3 --- /dev/null +++ b/app/validators/url_validator.rb @@ -0,0 +1,33 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +# frozen_string_literal: true + +# = URL Validator = +# +# In order to be considered valid, value must be: +# +# - parsable as an URI +# - use the http or https scheme +# - have a hostname +# - have a valid public TLD +# +class UrlValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + unless valid_web_address?(value) + record.errors[attribute] << (options[:message] || 'is an invalid Web URL') + end + end + + private + + def valid_web_address?(value) + uri = URI.parse(value) + uri.is_a?(URI::HTTP) && + uri.host.present? && + IANA::TLD.valid?(uri.host.split('.').compact.last) + rescue URI::InvalidURIError + false + end +end -- cgit v1.2.3