Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Where to put common functions & how to get user id

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


jakac at iprom

May 26, 2008, 11:13 PM

Post #1 of 7 (407 views)
Permalink
Where to put common functions & how to get user id

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)?

My table for logging this data is called "worklog" and contains
following columns:
* id
* date
* time
* user_id (integer)
* action (text)

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?

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.

My users table contains following columns:
* user_id
* username
* password
* first name, last name etc.......

user authenticates using his username&password just like in "Catalyst
authentication tutorial"
but my application always needs user's ID so I always have to search for
this user in the
database...

my $thisuser = $c->model('MyDB::Users')->find({username => $c->user->id});
my $thisuser_id = $thisuser->user_id;

Is there a simpler way to do it? I need current user's ID in almost
every controller all the time...

Thank you!
JC


arodland at comcast

May 26, 2008, 11:43 PM

Post #2 of 7 (393 views)
Permalink
Re: Where to put common functions & how to get user id [In reply to]

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

_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


jakac at iprom

May 27, 2008, 1:21 AM

Post #3 of 7 (389 views)
Permalink
Re: Where to put common functions & how to get user id [In reply to]

Andrew Rodland wrote:
> 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.
>
>
Thanks for the hint! I will take a look at it...

> $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.
>
>

Doesn't work for me ...

$c->log->debug("username: -".$c->user->id."- users ID -" .
$c->user->get("user_id") ."-");

just produces output:
[debug] username: -user1- users ID --
(user's ID is blank)

I am using following plugins:

use Catalyst qw/-Debug ConfigLoader Static::Simple
Session
Session::DynamicExpiry
Session::DynamicExpiry::Cookie
Session::State::Cookie
Session::Store::File
Authentication
Authentication::Store::DBIC
Authentication::Credential::Password
Authorization::Roles
Authorization::ACL
I18N
Unicode
/;



_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


omega at palle

May 27, 2008, 2:13 AM

Post #4 of 7 (385 views)
Permalink
Re: Where to put common functions & how to get user id [In reply to]

On May 27, 2008, at 10:21 AM, jakac wrote:

>
> Andrew Rodland wrote:
>> 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.
>>
>>
> Thanks for the hint! I will take a look at it...
>
>> $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.
>>
>>
>
> Doesn't work for me ...
>
> $c->log->debug("username: -".$c->user->id."- users ID -" .
> $c->user->get("user_id") ."-");

is user_id the name of your ID column in your schema (if your using
the DBIC store)?

get('column_name') is the syntax I think

- andreas


_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


perimus at gmail

May 27, 2008, 6:43 AM

Post #5 of 7 (379 views)
Permalink
Re: Where to put common functions & how to get user id [In reply to]

> My users table contains following columns:
> * user_id
> * username
> * password
> * first name, last name etc.......

the 'id' method is provided by DBIx::Class::PK, and is not one of your columns.
have you tried $c->user->user_id instead of $c->user->id?

> my $thisuser = $c->model('MyDB::Users')->find({username => $c->user->id});
> my $thisuser_id = $thisuser->user_id;

> Is there a simpler way to do it? I need current user's ID in almost every
> controller all the time...

The user object is already loaded by the authentication plugin. You
don't have to fetch it from the database again.

$thisuser = $c->user->obj;
$thisuser_id = $c->user->user_id
OR
$thisuser_id = $c->user->obj->user_id;


Hope this helps!

/Mitchell K. Jackson

_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


jakac at iprom

May 27, 2008, 7:45 AM

Post #6 of 7 (380 views)
Permalink
Re: Where to put common functions & how to get user id [In reply to]

Thanx! That did the trick - I never thought of it ...

So the summary is:
$c->user->get('column_name'); # doesn't work
$c->user->obj->column_name; # works!
$c->user->column_name; # works!

Thank you all for your help!

Mitch Jackson wrote:
>> My users table contains following columns:
>> * user_id
>> * username
>> * password
>> * first name, last name etc.......
>>
>
> the 'id' method is provided by DBIx::Class::PK, and is not one of your columns.
> have you tried $c->user->user_id instead of $c->user->id?
>
>
>> my $thisuser = $c->model('MyDB::Users')->find({username => $c->user->id});
>> my $thisuser_id = $thisuser->user_id;
>>
>
>
>> Is there a simpler way to do it? I need current user's ID in almost every
>> controller all the time...
>>
>
> The user object is already loaded by the authentication plugin. You
> don't have to fetch it from the database again.
>
> $thisuser = $c->user->obj;
> $thisuser_id = $c->user->user_id
> OR
> $thisuser_id = $c->user->obj->user_id;
>
>
> Hope this helps!
>
> /Mitchell K. Jackson
>
> _______________________________________________
> List: Catalyst[at]lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
>


dbix-class at trout

May 29, 2008, 4:04 AM

Post #7 of 7 (358 views)
Permalink
Re: Where to put common functions & how to get user id [In reply to]

On Tue, May 27, 2008 at 08:13:06AM +0200, 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)?
>
> My table for logging this data is called "worklog" and contains
> following columns:
> * id
> * date
> * time
> * user_id (integer)
> * action (text)
>
> 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');

Using &name for calling functions when you already know the name is perl 4.

Do NOT, EVER do it in perl 5.


> 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.

In Catalyst auth terms the 'id' is what the user uses to identify to the
app. It doesn't know about the rest of your DB schema.

> My users table contains following columns:
> * user_id
> * username
> * password
> * first name, last name etc.......
>
> user authenticates using his username&password just like in "Catalyst
> authentication tutorial"
> but my application always needs user's ID so I always have to search for
> this user in the
> database...

No you don't.

$c->user->obj returns the DBIC object.

So $c->user->obj->user_id will give you the user_id.

Although really, you shouldn't need that in your controller. Why do you
think you need a model-specific integer in code that's not supposed to
understand the model?

--
Matt S Trout Need help with your Catalyst or DBIx::Class project?
Technical Director http://www.shadowcat.co.uk/catalyst/
Shadowcat Systems Ltd. Want a managed development or deployment platform?
http://chainsawblues.vox.com/ http://www.shadowcat.co.uk/servers/

_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.