【问题标题】:Ruby on Rails setting two users as admin w/ ldap authenticationRuby on Rails 将两个用户设置为带有 ldap 身份验证的管理员
【发布时间】:2023-03-26 11:15:02
【问题描述】:

Ruby on Rails 的新手,我有一个显示页面,其中包含已从新页面填写的信息,包括允许或拒绝请求表单的单选按钮。我希望每个能够使用已设置的 ldap 身份验证登录的人都能够查看显示页面信息。但我只需要两个用户,即管理员,就可以看到允许或拒绝请求表单的单选按钮。我使用了许多不同的教程,而我在网上看到的类似问题并没有真正帮助我。 is_admin? articles_controllers 中的方法不是真正的方法,需要帮助。使用其 ldap 凭据用户名登录的用户采用“john_smith”的形式。我提前为我糟糕的编码技巧道歉。如果需要任何其他信息,我可以很快提供。

这是显示页面的代码。 app\views\articles\show.html.erb

<!DOCTYPE html>
<html>
<head>
<style>
p.inq
{
    width:400px; 
    word-wrap:break-word;;
}
</style>

</head>
<body>
<div id="logo"></div>
<center>
<h1> Submitted Request </h1>
<table><tr><p>

<td><strong>Approval Status:</strong></td>
 <td>  &nbsp &nbsp &nbsp       </td>  
 <td><%= @article.approve %></td></p></tr>
  <p><tr><td><strong>Name:</strong></td>
    <td>  &nbsp &nbsp &nbsp       </td>
    <td><%= @article.name %></td>
  </p></tr>
  <tr><p>
    <td><strong>Email:</strong></td>
    <td>       </td>
    <td><%= @article.email %></td>
  </p></tr>
  <tr> <p>
    <td><strong>Phone Number:</strong></td>
    <td>       </td>
    <td><%= @article.phone_number %></td>
  </p></tr>
    <tr><p>  
    <td><strong>Department Name</strong></td>
  <td>      </td>
  <td><%= @article.dept_name %></td>
  </p></tr>

      <tr><p>  
    <td><strong>Desc. of Business Need:</strong></td>
      <td>      </td>
  <td><%= @article.dob %></td>
  </p></tr>

  <tr><p>  
    <td><strong>Desc. of Changes to Firewall:</strong></td>
  <td>      </td>
  <td><%= @article.doc %></td>
  </p></tr>
    <tr><p>  

    <tr><p>  
    <td><strong>Additional Information(NAT's, VIPS's, Servers, etc.:</strong></td>
  <td>      </td>
  <td><%= @article.info %></td>
  </p></tr>

    <tr><p>
    <td><strong>Inquiry:</strong></td>
    <td>     </td>
    <td><p class="inq"><%= @article.inquiry %></p></td>
  </p></tr></table>
  <%= :username %>
  <% if is_admin? %>

  <%= form_for :article, :method => :patch, url: article_path(@article) do |f| %>
  <%= f.radio_button :approve, 'Approved' %> 
    <%= f.label :approve, 'Approve Request', :value => 'Approved' %> &nbsp &nbsp
    <%= f.radio_button :approve, 'Denied' %>
    <%= f.label :approve, 'Deny Request', :value => 'Denied' %>&nbsp &nbsp
     <%= f.radio_button :approve, 'Unapproved' %>
    <%= f.label :approve, 'Keep Request Unapproved', :value => 'Unapproved' %><br><br>
  <%= f.submit "Submit" %><br>
 <% end %><% end %>

  <%= link_to 'Back', articles_path %>
  </center></body>

app\models\user.rb

  class User < ActiveRecord::Base

    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable, :omniauthable

   def self.generate_random_password
        Digest::SHA1.hexdigest(Time.now.to_s)
    end 

    def create
        User.create(user_params)
    end  
    private 
    def user_params
        params.require(:user).permit(:firstname, :lastname, :displayname, :username, :email,       :password, :password_confirmation, :remember_me)
    end
  end

app\controllers\articles_controllers.rb

  class ArticlesController < ApplicationController
  def new
    @article = Article.new
  end

  def show  
    @article = Article.find(params[:id])
    end

  def create 
     @article = Article.new(article_params)

    respond_to do |format|
     if  @article.save
            FormMailer.confirmation_email(@article).deliver
            AdminMailer.confirmation_email(@article).deliver
            format.html { redirect_to @article, notice: 'successful' }
            format.json { render :show, status: :created, location: @article }
        #redirect_to @article
      else 
        format.html { render :new }
        format.json { render json: @article.errors, status: :unprocessable_entity }
        #render 'new'
    end
   end
  end 

  helper_method :is_admin?
  def is_admin?
      notsure == 'john_smith'
     end

  def index  
    @articles = Article.all 
  end

  def edit 
    @article = Article.find(params[:id])
    end

  def update
    @article = Article.find(params[:id])

    if @article.update(art_param)
        redirect_to root_path
    end
  end

  private 
  def article_params    
        params.require(:article).permit(:name, :email, :phone_number, :dept_name, :doc,             :inquiry, :dob, :info, :time)
  end

  def art_param
    params.require(:article).permit(:approve)
  end

  end

app\views\devise\sessions\new.html.erb

  <center><h2>Request Form - Sign In</h2>
  <% if user_signed_in? %>
      Welcome <%= current_user.email %> (<%= link_to "logout", destroy_user_session_path,              :method => :delete %>)
  <% else %>
      You are currently not logged in,  <%= link_to "Log In Here",       user_omniauth_authorize_path(:ldap) %>
  <% end %>

  <!-- <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
    <div><%= f.label :username %><br />
    <%= f.text_field :username, autofocus: true %></div>

    <div><%= f.label :password %><br />
      <%= f.password_field :password, autocomplete: "off" %></div>

    <% if devise_mapping.rememberable? -%>
      <div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
    <% end -%>

    <div><%= f.submit "Sign in" %></div>
  <% end %> 

  <%= render "devise/shared/links" %>-->
  </center>

【问题讨论】:

    标签: ruby-on-rails ruby devise ldap admin


    【解决方案1】:

    首先,你想要你的 is_admin 吗?在用户能够激活控制器中的任何操作之前触发的方法。这意味着,它应该是一个 before_filter 而不是一个辅助方法。现在,您可以填写您的 is_admin 了吗?方法,因此如果用户是管理员,它会执行您希望它执行的操作。这是一个如何为您工作的示例(下面,只有管理员可以使用 new 和 create 方法)。您可以随意修改它,因此如果用户尝试激活这些方法,它们会被重定向到其他地方或收到警报。

    before_filter :is_admin? , only: [:new, :create]
    
    def is_admin?
     return current_user
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      相关资源
      最近更新 更多