⚠ In case you've missed it we are migrating to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This forum is read-only and soon will be archived. ⚠


How to implement a permission system in GroceryCrud?


  • Please log in to reply
1 reply to this topic

#1 Ingus

Ingus

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 16 July 2015 - 12:14 AM

Hi!

 

I am new to CI 3 and groceryCRUD, started to implement a simple project management system for my own use.

I come from ASP.NET background and have not touched PHP in ~4 years.

 

Right now I want to implement a simple permission system.

 

Keys in DB are set up like this:

table projects:
int ProjectId;
int ManagerId;

table project_permissions:
int UserId;
int ProjectId;

table users:
int UserId;
string Name;

I wanted to filter projects where user has access permission like this:

 

$userId = $this->ion_auth->get_user_id();
$where = 'projects.ProjectId = project_permissions.ProjectId AND project_permissions.UserId = ' . $userId;
$this->grocery_crud->where($where);

 

The "where" does not work, because table project_permissions is unknown in the auto-generated query.

What is the standard way to implement this in CI?

 

Another question I have is, how to join two "where" statements in CI? For example, if I wanted the grid to show results of both 'where' statements:

$this->grocery_crud->where( 'UserId = ' . $userId); // $userId comes from session
$this->grocery_crud->where( 'UserId = 5'); //

Thanks.



#2 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts

Posted 16 July 2015 - 08:52 AM

Hi! If u have n_n relation so why in first table u have 2 keys? an usual case is table1, related_table_1_to_table_2, table2.

1)You can use custom model, where u extend your query.

2)You can extend  query right in controller, just ($this->db->select ->where.... )

3)Yes to "join" few where condition u just do what u did above, only it could be shorter - $this->grocery_crud->where('field1',$var1)->where('field2', $var2) or using array in where

$this->grocery_crud->where(array('field1' =>  $var1, 'field2' => $var2). But this is CI stuff, look at the docs Database Active Records class in CI documentation






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users