Welcome Guest, Not a member yet? Register   Sign In
Connect to the DB second time with different rights
#1

[eluser]Volder[/eluser]
I have a site where I have the following logic:

if the user is authorized - I connect to DB with 'logged_user' profile, otherwise I connect as 'guest'.
Of course, the difference is in rights, e.g. 'guest' can not delete records from tables etc.

I was using Native session library and I was deciding which user to connect depending on session variable 'logged_in'.

Then I have updated to using codeigniters session library with database usage.

So now I have the problem that before I could understand with which role to connect to database - I need to know whether user is logged in or on. And this could be understood from session variables. But for retrieving them I need to establish a connection first.

So it looks like a circle.

Is it possible to connect to DB with simple role which may only erase records from ci_sessions table. And then reconnect with another user role?

In the docs there is a reconnect() method - but it is not used with a parameter.

What would you suggest in my case?

thanks.
#2

[eluser]CroNiX[/eluser]
The first thing that popped into my head was to create additional connections in the database. Then just check the user permissions and use the appropriate db connection.
Code:
<?php
//in config/database.php
$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

//define new connection to be used by people with restricted access
$db['limited']['hostname'] = "localhost";
$db['limited']['username'] = 'some_other_user_with_limited_abilities';
$db['limited']['password'] = '';
$db['limited']['database'] = '';
$db['limited']['dbdriver'] = "mysql";
$db['limited']['dbprefix'] = "";
$db['limited']['pconnect'] = TRUE;
$db['limited']['db_debug'] = TRUE;
$db['limited']['cache_on'] = FALSE;
$db['limited']['cachedir'] = "";
$db['limited']['char_set'] = "utf8";
$db['limited']['dbcollat'] = "utf8_general_ci";

//Then I *believe* all you have to do in your controller/method is
$active_group = 'default';
$this->db->query('your_query_for_full_access_user');

//change to more restricted db user
$active_group = 'limited';
$this->db->query('your_query_for_limited_access_user');
#3

[eluser]Volder[/eluser]
I do have several connection types parametrized in DB config file.
The problem is that before I reach my controller - I am already connected to database by Session library.
And when I try to connect to DB with needed user - the statement is just ignored, because I am already connected.




Theme © iAndrew 2016 - Forum software by © MyBB