mardi 30 avril 2019

How to trigger different links to pages for different roles?

I am developing a website for different characters(or Roles) to use by using Ruby on Rails. Ruby: 2.6.0, Rails:5.2.2, Devise.
I want to create a webpage to show different country names. In 'index' page, it will show 'France', 'Switzerland' and 'China'. For different characters(or Roles), they have different permissions, ex: "Global Manager" can click 'France', 'Switzerland', 'China'; "Asia Manager" can only click 'China', and show 'No Permission!' if clicking 'France' or 'Switzerland'.

What I am trying now is using 'if...elsif...else' in 'index.html.erb' and 'countrycheck.html' with conditions of different email address domain. The problem I met is: I login in as 'test01@france.com'(Global Manager). I can see three countries' names in index page(correct), but I get the same page for 'France' title no matter which country I click. I know the error is because in my code 'france.com' matches 'France' page.

How to modify the codes? Is there any better way to realize the functionality? Thanks!

'Index.html.erb'

   <% if current_user.email.split("@").last == "france.com" && current_user.character != "Global Manager"%>
                 <br>
                 <br>
                 <li><%= link_to "France", "http://localhost:3000/countrycheck.html", :class => "choices" %></li>
            <% elsif current_user.email.split("@").last == "switzerland.com" && current_user.character != "Global Manager"%>
                 <br>
                 <br>
                 <li><%= link_to "Switzerland", "http://localhost:3000/countrycheck.html", :class => "choices" %></li>
            <% elsif current_user.email.split("@").last == "china.com" && current_user.character != "Global Manager"%>
                 <br>
                 <br>
                 <li><%= link_to "China", "http://localhost:3000/countrycheck.html", :class => "choices" %></li>
            <% elsif current_user.character == "Global Manager"%>
                 <br>
                 <br>
                 <li><%= link_to "France", "http://localhost:3000/countrycheck.html", :class => "choices" %></li>
                 <br>
                 <br>
                 <li><%= link_to "Switzerland", "http://localhost:3000/countrycheck.html", :class => "choices" %></li>
                 <br>
                 <br>
                 <li><%= link_to "China", "http://localhost:3000/maisoncheck.html", :class => "choices" %></li>
            <% else %>
                 <br>
                 <br>
                 <li><%= link_to "Other Countries", "http://localhost:3000/maisoncheck.html", :class => "choices" %></li>
            <% end %>

'countrycheck.html.erb'

<% if current_user.email.split("@").last == "france.com" %>
            <h1><%= link_to "France", "#"%></h1>
        <iframe>some iframe information related to France</iframe>
        <% elsif current_user.email.split("@").last == "switzerland.com" %>
            <h1><%= link_to "Switzerland", "#"%></h1>
        <iframe>some iframe information related to Swizterland</iframe>
        <% elsif current_user.email.split("@").last == "china.com" %>
            <h1><%= link_to "China", "#"%></h1>
       <iframe>some iframe information related to China</iframe>
        <% else %>
                <iframe> other countries iframe information </iframe>
        <% end %>

country_controller.rb

class CountryController < ApplicationController

    before_action :authenticate_user!
    before_action :check_admin

    def index
        @users = User.all
    end

    def show
        @users = User.all
    end

    def countrycheck
        if current_user
        else
            flash[:alert] = "No permission!"
            redirect_to "http://localhost:3000/country.html"
        end
    end

    protected

    def check_admin
        unless current_user.character == "Global Manager" || "Regional Manager"
          flash[:alert] = "No permission!"
          redirect_to "http://localhost:3000/introduction.html"
         return
        end
    end
end

Aucun commentaire:

Enregistrer un commentaire