From e5619547d5544a043dfec173f17020b09f882a98 Mon Sep 17 00:00:00 2001 From: hellekin Date: Thu, 11 Feb 2021 20:40:00 +0100 Subject: [FIX] Availability of agents --- app/controllers/agents_controller.rb | 10 +++++----- app/controllers/resources_controller.rb | 12 +++++++----- app/controllers/welcome_controller.rb | 21 ++++++++++++++------- app/helpers/application_helper.rb | 8 ++++++++ app/helpers/users_helper.rb | 2 +- app/javascript/scss/_agents.scss | 7 +++++++ app/models/resource.rb | 4 ++-- app/models/user.rb | 3 ++- app/views/agents/_agent.html.erb | 17 ++++++++++++++++- app/views/application/_user_info.html.erb | 4 ++++ app/views/layouts/application.html.erb | 2 +- app/views/taxonomies/_taxonomy.html.erb | 2 +- app/views/taxonomies/index.html.erb | 2 +- app/views/taxonomies/show.html.erb | 13 +++++++++++++ 14 files changed, 82 insertions(+), 25 deletions(-) (limited to 'app') diff --git a/app/controllers/agents_controller.rb b/app/controllers/agents_controller.rb index 105e443..fcbaebe 100644 --- a/app/controllers/agents_controller.rb +++ b/app/controllers/agents_controller.rb @@ -3,7 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0-or-later class AgentsController < ApplicationController - before_action :set_agent, only: [:new, :show, :edit, :update, :delete, :destroy] + before_action :set_agent, only: [:new, :show, :edit, :update, :delete, :destroy] # GET /agents def index @@ -26,7 +26,7 @@ class AgentsController < ApplicationController # GET /agents/:id/edit def edit - flash.now[:notice] = 'Please ask a maintainer to edit this resource!' unless current_user_maintainer? + flash.now[:notice] = 'Please ask a maintainer to edit this resource!' unless current_agent == @agent end # PATCH /agents/:id @@ -36,7 +36,7 @@ class AgentsController < ApplicationController # 2. Validate each change # 3. Moderate queue or save - return 403 unless current_user_maintainer? + return 403 unless current_agent == @agent respond_to do |format| if @agent.update(agent_params) @@ -51,7 +51,7 @@ class AgentsController < ApplicationController # GET /agents/:id/delete def delete - flash.now[:notice] = 'Please ask a maintainer to delete your Agent!' unless current_user_maintainer? + flash.now[:notice] = 'Please ask a maintainer to delete your Agent!' unless current_agent == @agent end # DELETE /agents/:id @@ -59,7 +59,7 @@ class AgentsController < ApplicationController return 403 # Yeah, right? # Check list # 1. User belongs to Agent and is :maintainer? - if !(current_user_maintainer? && current_user_leader?) + unless current_agent == @agent msg = 'You must be a maintainer and a leader to delete your Agent!' respond_to do |format| format.html { redirect_to :show, notice: msg } diff --git a/app/controllers/resources_controller.rb b/app/controllers/resources_controller.rb index 52d89ea..2fa392f 100644 --- a/app/controllers/resources_controller.rb +++ b/app/controllers/resources_controller.rb @@ -18,13 +18,15 @@ class ResourcesController < ApplicationController def create # TODO Background job to list similar items # TODO If there's a match, return to user with new record or list of mergeable ones - return 403 unless (current_user_editor? || current_user_observer?) classification = resource_params.delete(:classification) || { section_ids: [] } + Rails.logger.info resource_params + @resource = current_agent.resources.build(resource_params) respond_to do |format| + Rails.logger.info "format: #{format} - Res: #{@resource.inspect}" if @resource.save classification[:section_ids].each { |id| @resource.classifications.find_or_create_by(section_id: id) } format.html { redirect_to @resource, notice: 'Merci de votre contribution !' } @@ -43,8 +45,7 @@ class ResourcesController < ApplicationController # GET /resources/:id/edit def edit # TODO Add a moderation queue for unauthorized but valid changes - flash.now[:notice] = 'Please ask an editor or a maintainer to edit this resource!' unless (current_user_editor? || current_user_maintainer?) - + flash.now[:notice] = 'Please ask an editor or a maintainer to edit this resource!' unless @resource.agent == current_agent Rails.logger.info "EDIT: #{@resource.uuid} #{@resource.name} // #{current_agent.id}" end @@ -56,7 +57,8 @@ class ResourcesController < ApplicationController # 2. Validate each change # 3. Moderate queue or save - return 403 unless (current_user_editor? || current_user_maintainer?) + # TODO: pass this to current_agent and version resource + return 403 unless current_agent == @resource.agent respond_to do |format| if @resource.update(resource_params) @@ -78,7 +80,7 @@ class ResourcesController < ApplicationController def destroy # Check list # 1. User belongs to Agent and is :maintainer? - if !current_user_maintainer? + if @resource.agent != current_agent msg = 'You must be a maintainer to delete resources!' respond_to do |format| format.html { redirect_to :show, notice: msg } diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 5fce0cf..ad9cd95 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -102,20 +102,27 @@ class WelcomeController < ApplicationController # Set @current_user to existing or new User record from SSO user info def find_or_create_current_user - @current_user = User.find_by(external_id: @sso.user_info[:external_id]) || + user_data = { + external_id: @sso.user_info[:external_id], + avatar_url: @sso.user_info[:avatar_url], + email: @sso.user_info[:email], + name: @sso.user_info[:name], + username: @sso.user_info[:username] + } + + @current_user = User.find_by(external_id: user_data[:external_id]) || begin Rails.logger.info('new user...') - u = User.create( - external_id: @sso.user_info[:external_id], - avatar_url: @sso.user_info[:avatar_url], - email: @sso.user_info[:email], - name: @sso.user_info[:name], - username: @sso.user_info[:username]) + u = User.create(user_data) Rails.logger.info('created user %s' % u.inspect) u rescue Exception => e Rails.logger.warning("#{e.type}: #{e.message}") end + + user_data.reverse_merge!(@current_user.attributes.symbolize_keys) + @current_user.update(user_data) if user_data != @current_user.attributes + @current_user end # Update user agents diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index d80e4fe..c3c9b12 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3,6 +3,14 @@ # SPDX-License-Identifier: AGPL-3.0-or-later module ApplicationHelper + # Set CSS classes on body tag + def body_classes(*args) + classes = args || [] + classes << controller_name + classes << action_name + ' class="'.html_safe << classes.join(' ') << '"'.html_safe + end + # Markdown helper # Always use all extensions. Additional parser and render options may be # passed as a second argument. diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 1520238..4af2882 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -4,6 +4,6 @@ module UsersHelper def default_user_avatar_url - "https://talk.incommon.cc/uploads/talk_incommon_cc/optimized/1X/ae4533e882f427b1ae870080a42978c67c6437cc_2_32x32.png" + "https://talk.incommon.cc/uploads/default/original/1X/fb0b62dd9ad8917986ea0a901f864f04440de5f5.png" end end diff --git a/app/javascript/scss/_agents.scss b/app/javascript/scss/_agents.scss index ec9285d..951505a 100644 --- a/app/javascript/scss/_agents.scss +++ b/app/javascript/scss/_agents.scss @@ -7,3 +7,10 @@ // Place all the styles related to the Agents controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: https://sass-lang.com/ + +body.index article.agent { + border: 1px solid; + border-radius: 1rem; + margin: 2rem; + padding: 2rem; +} diff --git a/app/models/resource.rb b/app/models/resource.rb index c76a9c6..c603978 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -12,7 +12,7 @@ class Resource < ApplicationRecord has_many :classifications has_many :sections, through: :classifications - store_accessor :feature, :geometry, :properties + store_accessor :feature, :geometry, :properties, :meta, :version # validates_associated :agent @@ -77,7 +77,7 @@ class Resource < ApplicationRecord # Add IN COMMON Resource UUID property out['properties']['uuid'] = uuid # Add IN COMMON Agent UUID property - out['properties']['agent_uuid'] = agent.uuid + out['version'] = { agent: agent.uuid, resource: uuid, revision: 1 } out end end diff --git a/app/models/user.rb b/app/models/user.rb index 7288412..9e5c184 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,6 +9,7 @@ class User < ApplicationRecord include UsersHelper def avatar_url - attributes[:avatar_url] || default_user_avatar_url + attributes[:avatar_url].present? ? + attributes[:avatar_url] : '' # default_user_avatar_url end end diff --git a/app/views/agents/_agent.html.erb b/app/views/agents/_agent.html.erb index 5a74df1..fda8bd0 100644 --- a/app/views/agents/_agent.html.erb +++ b/app/views/agents/_agent.html.erb @@ -3,13 +3,28 @@

<%= link_to agent.name, agent_url(agent) %>

<%= link_to agent_url(agent) %>

+ <% if current_agent == agent %> +
+

Vous faƮtes partie de cet agent

+

Ainsi que <%= pluralize(agent.members.count, 'membre') %>.

+ +
+ <% end %>

Statistiques

+ +<% content_for :debug do %> +<%=tag.pre agent.inspect %> +<% end %> diff --git a/app/views/application/_user_info.html.erb b/app/views/application/_user_info.html.erb index 048e6fb..a1a42d2 100644 --- a/app/views/application/_user_info.html.erb +++ b/app/views/application/_user_info.html.erb @@ -11,3 +11,7 @@ <% end %> + +<% content_for :debug do %> +<%= @current_user.avatar_url %> +<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 60b285a..ce42bb6 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -13,7 +13,7 @@ <%= yield :head %> - + >

<%= link_to image_tag("https://talk.incommon.cc/uploads/default/original/1X/92f926e8210ea7a5412d6e2bdabfa825233e808b.png", alt: "IN COMMON", size: "x4em"), '/' %>

<%= render 'application/user_info' %> diff --git a/app/views/taxonomies/_taxonomy.html.erb b/app/views/taxonomies/_taxonomy.html.erb index eb2dd79..79df167 100644 --- a/app/views/taxonomies/_taxonomy.html.erb +++ b/app/views/taxonomies/_taxonomy.html.erb @@ -1,4 +1,4 @@ -
+

<%= taxonomy.name %>

<%= h taxonomy.summary %>

<%= m taxonomy.description %>
diff --git a/app/views/taxonomies/index.html.erb b/app/views/taxonomies/index.html.erb index 705b0d4..fcbcb8b 100644 --- a/app/views/taxonomies/index.html.erb +++ b/app/views/taxonomies/index.html.erb @@ -1,5 +1,5 @@

Available Taxonomies

- <%= render collection: @taxonomies %> + <%= render partial: :taxonomy, collection: @taxonomies %>
diff --git a/app/views/taxonomies/show.html.erb b/app/views/taxonomies/show.html.erb index 99e9c89..3971acf 100644 --- a/app/views/taxonomies/show.html.erb +++ b/app/views/taxonomies/show.html.erb @@ -1 +1,14 @@ +<%#= map_container %> + <%= taxonomy_filter %> + +<% content_for :aside do %> +
+
+ +
+
+
+<% end %> + + -- cgit v1.2.3