Page 1 of 1

Help! Need to filter value lists based on logged in user

PostPosted: Mon Jul 27, 2009 11:02 am
by yokaiface
Hi

I have looked for the links on the above topic but none seems to be clear enough for my understanding. Please help me figure out this. Basically, I have three tables in the scenario. One for user accounts , one for domains and one for users under the domain. My valuelist field displays domain records in the users table but I want to restrict the displayed list based on who is logged in.

Any Ideas?

PostPosted: Mon Jul 27, 2009 4:50 pm
by shannah
You'll want to use a dynamic valuelist then. In your application delegate class, define a method such as :

Code: Select all
function valuelist__domains(){
    static $values = 0;
    if ( !is_array($values) ){
        $auth =& Dataface_AuthenticationTool::getInstance();
        $user =& $auth->getLoggedInUser();
        $sql = "select domainid, domainname from domains";
        if ( $user ){
            $sql = ." where owner='".addslashes($user->val('username'))."'";
            $res = mysql_query($sql, df_db());
            $values = array();
            while ($row = mysql_fetch_row($res) ) $values[$row[0]] = $row[1];
            @mysql_free_result($res);
       
        } else {
             $values = array();
        }
    }


    return $values;
}



The idea is that you return a different array depending on who is logged in.

-Steve

PostPosted: Tue Jul 28, 2009 6:06 am
by yokaiface
Hi Steve,

Thanks for your prompt reply, however, I still have some problem implementing that code.

Below are the tables in question

CREATE TABLE IF NOT EXISTS `virtual_domains` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
`account_Number` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
)

CREATE TABLE IF NOT EXISTS `virtual_users` (
`id` int(11) NOT NULL auto_increment,
`domain_id` int(11) NOT NULL,
`user` varchar(40) NOT NULL,
`password` varchar(32) NOT NULL,
`account_Number` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQUE_EMAIL` (`domain_id`,`user`)
)


CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(11) NOT NULL auto_increment,
`account_Number` varchar(10) NOT NULL,
`Name` varchar(30) NOT NULL,
`contact_Number` varchar(15) NOT NULL,
`profile` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
`Role` enum('READ ONLY','EDIT','NO ACCESS','ADMIN') NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `account_Number` (`account_Number`)
)


You could see that I have account_Number in all of the tables. Below is the corresponding method I tried to implement on the virtual_users table on the field domain_id. Actually I want the dynamic values list "domains" on the domain_id field.


function valuelist__domains(){
static $values = 0;
if ( !is_array($values) ){
$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser();
$sql = "select id, name from virtual_domains";
if ( $user ){
$sql = ." where accountNumber='".addslashes($user->val('account_Number'))."'";
$res = mysql_query($sql, df_db());
$values = array();
while ($row = mysql_fetch_row($res) ) $values[$row[0]] = $row[1];
@mysql_free_result($res);

} else {
$values = array();
}
}


return $values;
}


The username_column for the login is account_Number in the accounts table. Below is my field.ini file for the virtual_users table. With the implementation of the above method I get a blank page. What am I missing?

[domain_id]
widget:label = "Domain Name"
widget:type = select
vocabulary = domains
[user]
widget:label = "Account"
[account_Number]
visibility = hidden

PostPosted: Tue Jul 28, 2009 11:55 am
by shannah
You need to do some debugging. E.g. it is possible that your SQL query is failing.

Add if ( !$res ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
after your mysql query call to display the error if it is ocurring there. If not there, then do some debugging to figure out where the error is occurring (e.g. with echo statements and exit statements at break points.

-Steve

PostPosted: Wed Jul 29, 2009 9:20 am
by yokaiface
Thanks Steve, I modified the method a little and its working now.

Another issue just surfaced. I have the error when trying to log into my application

Fatal error: Cannot use object of type PEAR_Error as array in /var/vmail/xataface/Dataface/Serializer.php on line 75

What could be the problem?

Regards

PostPosted: Wed Jul 29, 2009 9:34 am
by yokaiface
Hi Steve,

I'm Ok now. It was a typo for the username column for authentication