Help! Need to filter value lists based on logged in user
Archived from the Xataface Users forum.
yokaiface — Mon Jul 27, 2009 11:02 am
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?
shannah — Mon Jul 27, 2009 4:50 pm
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
yokaiface — Tue Jul 28, 2009 6:06 am
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
shannah — Tue Jul 28, 2009 11:55 am
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
yokaiface — Wed Jul 29, 2009 9:20 am
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
yokaiface — Wed Jul 29, 2009 9:34 am
Hi Steve,
I’m Ok now. It was a typo for the username column for authentication