gpt4 book ai didi

php - 多个管理员的 Codeigniter 角色

转载 作者:行者123 更新时间:2023-11-29 13:43:36 26 4
gpt4 key购买 nike

大家好,我需要有关 codeigniter 角色或权限的帮助。我有一个用户角色(管理员):

数据库中的表用户:

id   int(11)    
email varchar(100)
password varchar(128)
name varchar(100)

在我的管理面板中,我有(page.php Controller )=页面管理,页面顺序,(agent.php Controller )=添加,编辑,删除...,(gyms)=添加,编辑,删除... ,(article.php Controller )

我有 21 个部分,对于每个部分我有不止一种处理,我想要的是为每个部分分配一个管理员,只能编辑和查看他的部分。所以我将有 21 个section_admin 和一个(或多个)global_admin 来管理一切

我在用户表中添加了一个名为 type 的其他字段:类型 varchar(50)它将有两个值section_admin 或global_admin。我进行了搜索,但没有找到任何教程告诉我如何做到这一点。

我不知道如何将角色管理集成到我的系统中。有人能帮我吗?

Controller :user.php

    class User extends Admin_Controller
{

public function __construct ()
{
parent::__construct();
}

public function index ()
{
// Fetch all users
$this->data['users'] = $this->user_m->get();

// Load view
$this->data['subview'] = 'admin/user/index';
$this->load->view('admin/_layout_main', $this->data);
}

public function edit ($id = NULL)
{
// Fetch a user or set a new one
if ($id) {
$this->data['user'] = $this->user_m->get($id);
count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
}
else {
$this->data['user'] = $this->user_m->get_new();
}

// Set up the form
$rules = $this->user_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);

// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->user_m->array_from_post(array('name', 'email', 'password'));
$data['password'] = $this->user_m->hash($data['password']);
$this->user_m->save($data, $id);
redirect('admin/user');
}

// Load the view
$this->data['subview'] = 'admin/user/edit';
$this->load->view('admin/_layout_main', $this->data);
}

public function delete ($id)
{
$this->user_m->delete($id);
redirect('admin/user');
}

public function login ()
{
// Redirect a user if he's already logged in
$dashboard = 'admin/dashboard';
$this->user_m->loggedin() == FALSE || redirect($dashboard);

// Set form
$rules = $this->user_m->rules;
$this->form_validation->set_rules($rules);

// Process form
if ($this->form_validation->run() == TRUE) {
// We can login and redirect
if ($this->user_m->login() == TRUE) {
redirect($dashboard);
}
else {
$this->session->set_flashdata('error', 'That email/password combination does not exist');
redirect('admin/user/login', 'refresh');
}
}

// Load view
$this->data['subview'] = 'admin/user/login';
$this->load->view('admin/_layout_modal', $this->data);
}

public function logout ()
{
$this->user_m->logout();
redirect('admin/user/login');
}

public function _unique_email ($str)
{
// Do NOT validate if email already exists
// UNLESS it's the email for the current user

$id = $this->uri->segment(4);
$this->db->where('email', $this->input->post('email'));
!$id || $this->db->where('id !=', $id);
$user = $this->user_m->get();

if (count($user)) {
$this->form_validation->set_message('_unique_email', '%s should be unique');
return FALSE;
}

return TRUE;
}
}

模型 user_m.php :

                protected $_table_name = 'users';
protected $_order_by = 'name';
public $rules = array(
'email' => array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|xss_clean'
),
'password' => array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required'
)
);
public $rules_admin = array(
'name' => array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|xss_clean'
),
'email' => array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|callback__unique_email|xss_clean'
),
'password' => array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|matches[password_confirm]'
),
'password_confirm' => array(
'field' => 'password_confirm',
'label' => 'Confirm password',
'rules' => 'trim|matches[password]'
),
);

function __construct ()
{
parent::__construct();
}

public function login ()
{
$user = $this->get_by(array(
'email' => $this->input->post('email'),
'password' => $this->hash($this->input->post('password')),
), TRUE);

if (count($user)) {
// Log in user
$data = array(
'name' => $user->name,
'email' => $user->email,
'id' => $user->id,
'loggedin' => TRUE,
);
$this->session->set_userdata($data);
}
}

public function logout ()
{
$this->session->sess_destroy();
}

public function loggedin ()
{
return (bool) $this->session->userdata('loggedin');
}

public function get_new(){
$user = new stdClass();
$user->name = '';
$user->email = '';
$user->password = '';
return $user;
}

public function hash ($string)
{
return hash('sha512', $string . config_item('encryption_key'));
}
}

最佳答案

有太多方法可以将权限系统合并到您的项目中,这完全取决于您的需要。如果我正确理解了您的问题,我将为您提供有关您的案例的基本想法,我将如何做:

  1. 是的,您可以向用户表添加另一个字段并将其命名为角色
  2. 向您的节表添加一个 user_id 字段。这就是将用户与部分连接起来的方式。
  3. 用户登录后,请检查该用户是否为section_user,如果是,则需要根据该 user_id 从数据库中提取正确的部分。
  4. 如果不是,则表示它是全局管理员,然后显示所有部分。

我不确定我是否正确理解了你的问题。

请告诉我。

关于php - 多个管理员的 Codeigniter 角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17757462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com