Commit | Line | Data |
7bde9079 |
1 | package DBIx::Class::UserStamp; |
2 | |
3 | use base qw(DBIx::Class); |
4 | |
5 | use warnings; |
6 | use strict; |
50fd24f3 |
7 | use Carp qw/ confess /; |
7bde9079 |
8 | |
ac6874ee |
9 | our $VERSION = '0.11'; |
7bde9079 |
10 | |
bb500ec5 |
11 | __PACKAGE__->load_components( qw/DynamicDefault/ ); |
7bde9079 |
12 | |
13 | =head1 NAME |
14 | |
15 | DBIx::Class::UserStamp - Automatically set update and create user id fields |
16 | |
17 | =head1 DESCRIPTION |
18 | |
19 | Automatically set fields on 'update' and 'create' that hold user id |
20 | values in a table. This can be used for any user id based |
21 | field that needs trigger like functionality when a record is |
22 | added or updated. |
23 | |
24 | =head1 SYNOPSIS |
25 | |
26 | package MyApp::Schema; |
27 | |
50fd24f3 |
28 | __PACKAGE__->mk_group_accessors('simple' => qw/current_user/); |
7bde9079 |
29 | |
30 | |
31 | package MyApp::Model::MyAppDB; |
32 | use Moose; |
0b751b44 |
33 | use namespace::autoclean; |
7bde9079 |
34 | |
0b751b44 |
35 | extends 'Catalyst::Model::DBIC::Schema'; |
7bde9079 |
36 | |
50fd24f3 |
37 | __PACKAGE->config( |
38 | traits => ['WithCurrentUser'], # Requires Catalyst::TraitFor::Model::DBIC::Schema::WithCurrentUser |
39 | ); |
7bde9079 |
40 | |
41 | package MyApp::Schema::SomeTable; |
42 | |
43 | __PACKAGE__->load_components(qw( UserStamp ... Core )); |
0b751b44 |
44 | |
7bde9079 |
45 | __PACKAGE__->add_columns( |
46 | id => { data_type => 'integer' }, |
47 | u_created => { data_type => 'int', store_user_on_create => 1 }, |
48 | u_updated => { data_type => 'int', |
49 | store_user_on_create => 1, store_user_on_update => 1 }, |
50 | ); |
51 | |
52 | Now, any update or create actions will update the specified columns with the |
0b751b44 |
53 | current user_id, using the current_user_id accessor. |
7bde9079 |
54 | |
0b751b44 |
55 | This is effectively trigger emulation to ease user id field insertion |
7bde9079 |
56 | |
57 | =cut |
58 | |
59 | sub add_columns { |
bb500ec5 |
60 | my ($self, @cols) = @_; |
61 | my @columns; |
7bde9079 |
62 | |
bb500ec5 |
63 | while (my $col = shift @cols) { |
64 | my $info = ref $cols[0] ? shift @cols : {}; |
7bde9079 |
65 | |
bb500ec5 |
66 | if ( delete $info->{store_user_on_update} ) { |
67 | $info->{dynamic_default_on_update} = 'get_current_user_id'; |
68 | } |
69 | if ( delete $info->{store_user_on_create} ) { |
70 | $info->{dynamic_default_on_create} = 'get_current_user_id'; |
71 | } |
7bde9079 |
72 | |
bb500ec5 |
73 | push @columns, $col => $info; |
7bde9079 |
74 | } |
75 | |
bb500ec5 |
76 | return $self->next::method(@columns); |
7bde9079 |
77 | } |
78 | |
79 | =head1 METHODS |
80 | |
81 | =head2 get_current_user_id |
82 | |
50fd24f3 |
83 | This method is meant to be overridden. The default is to look for a |
84 | 'current_user' accessor on the schema, and call an ->id method |
85 | on it. |
7bde9079 |
86 | |
87 | =cut |
50fd24f3 |
88 | |
89 | sub get_current_user_id { |
90 | my $self = shift; |
91 | my $schema = $self->result_source->schema; |
92 | if ($schema->can('current_user')) { |
93 | return $schema->current_user->id |
94 | if $schema->current_user; |
95 | } |
96 | elsif ($schema->can('current_user_id')) { |
97 | return $schema->current_user_id; |
98 | } |
99 | else { |
100 | confess("Your schema $schema does not have either a 'current_user' or 'current_user_id' accessors needed by " . __PACKAGE__); |
101 | } |
102 | } |
7bde9079 |
103 | |
104 | =head1 AUTHOR |
105 | |
106 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
107 | |
c597fbb8 |
108 | =head1 CONTRIBUTORS |
7bde9079 |
109 | |
110 | John Goulah <jgoulah@cpan.org> |
c597fbb8 |
111 | Florian Ragwitz <rafl@debian.org> |
7bde9079 |
112 | |
113 | =head1 COPYRIGHT |
114 | |
115 | This program is free software; you can redistribute |
116 | it and/or modify it under the same terms as Perl itself. |
117 | |
118 | =cut |
119 | |
120 | 1; |