使用单个模型错误进行设计和Active Admin

时间:2018-12-05 05:44:56

标签: ruby-on-rails ruby devise activeadmin

我的应用程序为用户的Devise和Active Admin都需要一个模型,我想根据Profile模型中存在的角色来区分超级管理员,admin和用户,因此我遵循了this link by dan doezema。请帮助我,因为我刚接触Rails世界,不知道该怎么办。 我已经删除了默认创建的管理员用户。我在app / admin文件夹中的user.rb文件是:

$ valgrind ./bin/struct_alloc_member
==15553== Memcheck, a memory error detector
==15553== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==15553== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==15553== Command: ./bin/struct_alloc_member
==15553==
succeeded:
 iNum: 50
 base: 6
 str : created with intToNumber (50, 6)
==15553==
==15553== HEAP SUMMARY:
==15553==     in use at exit: 0 bytes in 0 blocks
==15553==   total heap usage: 2 allocs, 2 frees, 49 bytes allocated
==15553==
==15553== All heap blocks were freed -- no leaks are possible
==15553==
==15553== For counts of detected and suppressed errors, rerun with: -v
==15553== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

在这里我使用了User,因为我想使用由设备生成的User。我在app / admin中的users.rb文件是:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct {
    int iNum;   /* automatic storage types */
    int base;
    char *sNum; /* a pointer - hold address to something else  */
} number;       /*             must point to valid memory addr */

number *intToNumber (int nnum, int nbase) {

    number *theNumbers;

    theNumbers= malloc (sizeof *theNumbers);    /* allocate */

    if (theNumbers == NULL) {                   /* validate */
        perror ("malloc-theNumbers");
        return NULL;
    }

    theNumbers->iNum = nnum;    /* initialize values */
    theNumbers->base = nbase;
    theNumbers->sNum = NULL;

    return theNumbers;          /* return pointer */
}

/* allocate/set sNum member of n to s */
char *setsNum (number *n, const char *s)
{
    if (!n) {   /* validate n not NULL, return NULL on failure */
        fputs ("error: struct parameter 'n' NULL.\n", stderr);
        return NULL;
    }
    size_t len = strlen (s);        /* get length */

    n->sNum = malloc (len + 1);     /* allocate storage (+1 byte) */

    if (!n->sNum) {                 /* validate allocation */
        perror ("malloc-sNum");
        return NULL;
    }

    memcpy (n->sNum, s, len + 1);   /* copy s to new block of memory */

    return n->sNum;                 /* return pointer (convenience) */
}

int main (void) {

    number *mynum = intToNumber (50, 6);    /* declare/initialize mynum */

    if (!mynum)     /* validate succeeded */
        return 1;

    /* allocate/validate mynum->sNum, copy string */
    if (!setsNum (mynum, "created with intToNumber (50, 6)")) {
        free (mynum);
        return 1;
    }
    /* output values held in mynum */
    printf ("succeeded:\n iNum: %d\n base: %d\n str : %s\n",
            mynum->iNum, mynum->base, mynum->sNum);

    free (mynum->sNum); /* free allocated string */
    free (mynum);       /* free struct */
}

我没有在app / admin中更改dashboard.rb文件。 application_controller.rb文件为:

    ActiveAdmin.register User do
        form do |f|
            f.inputs "User Details" do
                f.input :email
            end
            f.buttons
        end
    end

我已从route.rb文件中删除了以下路由

    ActiveAdmin.register User do
        permit_params :email
        index do
            selectable_column
            id_column
            column :email
            column :current_sign_in_at
            column :sign_in_count
            column :created_at
            actions
        end
        filter :email
        filter :current_sign_in_at
        filter :sign_in_count
        filter :created_at
        form do |f|
            f.inputs do
                f.input :email
                end
                f.actions
            end
        end

我还破坏了AdminUser模型

1 个答案:

答案 0 :(得分:1)

您可以在config/initializers/active_admin.rb中配置Active Admin设置。参见Active Admin Authentication

具体来说,您需要设置:config.authentication_methodconfig.current_user_method以匹配您在ApplicationController中定义的方法。

示例:

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  protected

  def authenticate_admin_user!
    authenticate_user!
    unless current_user.profile.role == 'super_admin'
      flash[:alert] = "Unauthorized Access!"
      redirect_to root_path
    end
  end

  def current_admin_user
    return unless current_user&.profile.role == 'super_admin'
    current_user
  end
end

config / initializers / active_admin.rb

config.authentication_method = :authenticate_admin_user!
config.current_user_method = :current_admin_user

或者,您可以仅通过角色认证当前用户并授权管理视图。参见Active Admin Authorization Adapter