[Catalyst] Where to put common functions & how to get user id
Andrew Rodland
arodland at comcast.net
Tue May 27 07:43:25 BST 2008
On Tuesday 27 May 2008 01:13:06 am jakac wrote:
> Hello!
>
> Can someone please explain where should I put common functions such as
> "action logging" (my app is designed so that it actually logs any
> inserts/updates
> of data)?
>
> [...]
> in my older (non-catalyst) application I had a common function defined as:
>
> sub workLog {
> my ($dbh,$user_id,$action) = @_;
> return $dbh->do("insert into worklog user_id,action values
> ('$user_id','$action');");
> }
>
> And whenever I needed to log something I just used a call like:
> &workLog($dbh,$user_id, ' User updated table foo column bar');
>
> So now I am wondering how to simply implement a call like this into
> catalyst?
>
Create a model for the log (it could be a C::M::DBIC::Schema, your own custom
class, or something else) and then you can
$c->model('ActionLog')->logaction($userid, $action) assuming an appropriate
logaction method.
> Also the other thing is - how to get active user's ID? When I
> authenticate my user
> using Authentication plugin (using $c->login($username,$password)) the
> $c->user->id
> contains that user's username and not his ID.
> [...]
> Is there a simpler way to do it? I need current user's ID in almost
> every controller all the time...
>
$c->user->get("user_id") etc. which is part of the universal authentication
interface. The DBIC store also provides $c->user->obj returns the underlying
object directly, but you probably don't need that. There are examples in
perldoc Catalyst::Plugin::Authentication.
Andrew
More information about the Catalyst
mailing list