how to automatically populate user_Id

A place for users and developers of the Xataface to discuss and receive support.

Postby Markus » Wed May 09, 2007 4:26 am

Hi Steve,

this is my first posting. So let me thank you for your great work with the dataface framework first.

I have a question on how to populate a given variable (here the $ENV.Username) into a field automatically.

I have some tables connected with foreign keys and want the $ENV.Username variable of the current User to be inserted into a field widget when inserting a new record.

Thank you

Markus
Markus
 
Posts: 94
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Wed May 09, 2007 8:26 am

Hi Markus,

Thanks for the question.Ê There are a couple of ways to accomplish this.Ê You can either set the default field value to the user id, or you can set the value in the beforeSave() trigger.

I generally prefer the first method.Ê For this, you would use the fieldname__default() method in the delegate class:

e.g.Ê Suppose the name of the field we want to populate is username:

function username__default(){
ÊÊÊ $auth =& Dataface_AuthenticationTool::getInstance();
ÊÊÊ return $auth->getLoggedInUsername();
}

Then it is a good idea to set the widget for this field to hidden so that it can't be changed (or make it so that only administrators have edit permissions for this field).



The second option (setting it in the beforeSave() trigger) would look like:

function beforeSave(&$record){
ÊÊÊ $auth =& Dataface_AuthenticationTool::getInstance();
ÊÊÊ $record->setValue('username', $auth->getLoggedInUsername());
}

(Note that the beforeSave() trigger will only work for this in 0.7 and later.Ê In 0.6.x changes made to the record in the triggers weren't propogated to the database).



Hope this helps.


-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby Markus » Wed May 09, 2007 9:21 am

Hi Steve,

I tried the first method and it all works fine. :) Thanks a lot for your help.

Markus
Markus
 
Posts: 94
Joined: Wed Dec 31, 1969 5:00 pm

Postby dwisn » Fri May 11, 2007 11:14 am

Thanks for the question.Ê There are a couple of ways to accomplish this.Ê You can either set the default field value to the user id, or you can set the value in the beforeSave() trigger.

I generally prefer the first method.Êp>
-Steve



Steve, I am assuming that this implies that you are using "authentication".

Is there a 'place' I can pass in the 'REMOTE_USER' from the Browser and be able to use this as the "login" name for use within history tables?

Since I require SecurID to my site, I really hate ( well, I don't... but you know how people hate to double authenticate" ) - to require somebody to ..

So, I would like to bring in whatever variables I need/use from the Browser. I'm just not sure.. where to pass them to.

In Advance, Thanks
dan
dwisn
 
Posts: 25
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Fri May 11, 2007 12:04 pm

Hi Dan,

Since PHP makes the REMOTE_USER variable available to you in the $_SERVER array, you can use this in place of the $auth->getLoggedInUsername() call.
e.g.
function username__default(){
ÊÊÊ return $_SERVER['REMOTE_USER'];
}
-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby dwisn » Fri May 11, 2007 12:17 pm

Hi Dan,

Since PHP makes the REMOTE_USER variable available to you in the $_SERVER array, you can use this in place of the $auth->getLoggedInUsername() call.
e.g.
function username__default(){
ÊÊÊ return $_SERVER['REMOTE_USER'];
}
-Steve



Hey...
Why can't I be smart like you?
dwisn
 
Posts: 25
Joined: Wed Dec 31, 1969 5:00 pm

Postby dwisn » Mon May 14, 2007 6:48 am

Actaully, I think I was able to do it by enabling history... and then.

Down around line 125

$auth =& Dataface_AuthenticationTool::getInstance();
$userRecord =& $auth->getLoggedInUser();
if ( !isset($userRecord) ){
// $user = null;
$user = $_SERVER['REMOTE_USER'];
} else {
// $user = $auth->getLoggedInUsername();
$user = $_SERVER['REMOTE_USER'];

}


dan
dwisn
 
Posts: 25
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Mon May 14, 2007 10:10 am

I suppose this is one way to do it.Ê The risk here is that by changing the code in the dataface distribution it will be more difficult to upgrade later.Ê The best way to support this sort of authentication is to create an authentication handler for itÊ (see the CAS module for a sample of an authentication handler).Ê The handler would really only need to implement one method: getLoggedInUsername().

But if this one is working for you you can go with it.

Best regards

Steve

--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby dwisn » Mon May 14, 2007 10:29 am

I'll change it; since, of course "it would be difficult" to upgrade later and track -
thanks -
dan
dwisn
 
Posts: 25
Joined: Wed Dec 31, 1969 5:00 pm

Postby dwisn » Mon May 14, 2007 11:04 am

Actually... I continue to be slightly confused.

1) If I do not want to authenticate, and only want to grab the "remote_user" from the Browser, am I correct in believing that I do not need [auth] in the config.

eg:
# [_auth]
# users_table = Users
# username_column = UserName
# password_column = Password

But since I want to track History, that I enable History.

[history]
enabled = 1

That being said... Do I need to Have _auth set and point it towards CAS and change the default handler as you mentioned in your first reply..

I guess I may be missing the interaction between having auth set ( in the config file ) and history enabled. I know HistoryTool.php has:

$auth =& Dataface_AuthenticationTool::getInstance();
$userRecord =& $auth->getLoggedInUser();
if ( !isset($userRecord) ){
$user = null;
} else {
$user = $auth->getLoggedInUsername();
}

But a few posts back, you suggested to use username_default instead of getLoggedInUsername..

I'm getting confused on "what class calls..... what function interacts... " .


Luckily..... I have a nice development box to try everything out with... I"ve downloaded CAS module and am looking through it..


But let me query you about the basic starting point for the default_username from the browser.

Do I need AUTH enabled? ( as you suggest by the use of the CAS module)?
or turn AUTH off, Leave History enabled - and pass it when a Record is updated? ( ie actions? )

Monday woes....
dan
dwisn
 
Posts: 25
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 22 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved