diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb index 8958582..d7d1362 100644 --- a/app/controllers/roles_controller.rb +++ b/app/controllers/roles_controller.rb @@ -1,77 +1,50 @@ class RolesController < ApplicationController + respond_to :html after_action :verify_authorized def create @role = Role.new(params[:role]) authorize @role - access_level = params[:role][:access_level].to_i - if access_level >= 3 then - @role.administrator = true - end - if access_level >= 2 then - @role.editor = true - end - if (user_signed_in?) && @role.plan.administerable_by(current_user.id) then - respond_to do |format| - if params[:role][:email].present? && params[:role][:email].length > 0 then - message = I18n.t('helpers.project.user_added') - if @role.save - if @role.user.nil? then - if User.find_by_email(params[:role][:email]).nil? then - User.invite!(email: params[:role][:email]) - message = I18n.t('helpers.project.invitation_success') - @role.user = User.find_by_email(params[:role][:email]) - @role.save - else - @role.user = User.find_by_email(params[:role][:email]) - @role.save - UserMailer.sharing_notification(@role).deliver - end - else - UserMailer.sharing_notification(@role).deliver - end - flash[:notice] = message - format.html { redirect_to controller: 'plans', action: 'share', id: @role.plan.slug } - else - format.html { render action: "new" } - end - else - flash[:notice] = I18n.t('helpers.project.enter_email') - format.html { redirect_to controller: 'plans', action: 'share', id: @role.plan.slug } - end - end + @role.access_level = params[:role][:access_level].to_i + if params[:role][:email].present? + message = I18n.t('helpers.project.user_added') + if @role.save + if @role.user.nil? then + if User.find_by_email(params[:role][:email]).nil? then + User.invite!(email: params[:role][:email]) + message = I18n.t('helpers.project.invitation_success') + @role.user = User.find_by_email(params[:role][:email]) + @role.save + else + @role.user = User.find_by_email(params[:role][:email]) + @role.save + UserMailer.sharing_notification(@role).deliver + end + else + UserMailer.sharing_notification(@role).deliver + end + flash[:notice] = message + redirect_to controller: 'plans', action: 'share', id: @role.plan.slug + else + render action: "new" + end else - render(file: File.join(Rails.root, 'public/403.html'), status: 403, layout: false) + flash[:notice] = I18n.t('helpers.project.enter_email') + redirect_to controller: 'plans', action: 'share', id: @role.plan.slug end end def update @role = Role.find(params[:id]) authorize @role - access_level = params[:role][:access_level].to_i - if access_level >= 3 then - @role.administrator = true - else - @role.administrator = false - end - if access_level >= 2 then - @role.editor = true - else - @role.administrator = false - end - if (user_signed_in?) && @role.plan.administerable_by(current_user.id) then - respond_to do |format| - if @role.update_attributes(params[:role]) - flash[:notice] = I18n.t('helpers.project.sharing_updated') - UserMailer.permissions_change_notification(@role).deliver - format.html { redirect_to controller: 'plans', action: 'share', id: @role.plan.slug } - else - format.html { render action: "edit" } - end + @role.access_level = params[:role][:access_level].to_i + if @role.update_attributes(params[:role]) + flash[:notice] = I18n.t('helpers.project.sharing_updated') + UserMailer.permissions_change_notification(@role).deliver + redirect_to controller: 'plans', action: 'share', id: @role.plan.slug + else + render action: "edit" end - else - render(:file => File.join(Rails.root, 'public/403.html'), status: 403, layout: false) - end end def destroy diff --git a/app/models/role.rb b/app/models/role.rb index 4125fda..282802d 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -42,6 +42,7 @@ unless User.find_by(email: email).nil? then user = User.find_by(email: email) end + self.save! end ## @@ -52,13 +53,14 @@ # # @return [Integer] def access_level - if project_administrator then + if self.administrator? then return 3 - elsif project_editor then + elsif self.editor? then return 2 else return 1 end + self.save! end ## @@ -70,14 +72,15 @@ def access_level=(new_access_level) new_access_level = new_access_level.to_i if new_access_level >= 3 then - project_administrator = true + self.administrator = true else - project_administrator = false + self.administrator = false end if new_access_level >= 2 then - project_editor = true + self.editor = true else - project_editor = false + self.editor = false end + self.save! end end diff --git a/app/views/projects/share.html.erb b/app/views/projects/share.html.erb index 22a3a32..cec0df7 100644 --- a/app/views/projects/share.html.erb +++ b/app/views/projects/share.html.erb @@ -6,16 +6,16 @@ <%= render :partial => "project_nav_tabs", locals: {project: @project, active: "share_project"} %>
| <%= group.user.name %> | @@ -33,7 +33,7 @@ <% if group.project_creator then %> <%= t("helpers.project.share.owner")%> <% else %> - <%= form_for group, :url => {:controller => :project_groups, :action => :update, :id => group.id }, :html=>{:method=>:put} do |f| %> + <%= form_for group, :url => {:controller => :roles, :action => :update, :id => group.id }, :html=>{:method=>:put} do |f| %> <%= f.select :access_level, {t('helpers.project.share.co_owner') => 3, t('helpers.project.share.edit') => 2, t('helpers.project.share.read_only') => 1}, {}, {:id => "#{group.id}-can-edit", :class => "toggle-existing-user-access has-tooltip", 'data-toggle' => "tooltip", 'title' => t('helpers.project.share.permissions_desc') } %> <% end %> <% end %> @@ -49,14 +49,14 @@
|---|