lib/Rapi/Blog/DB/Result/User.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 204 CC 13 SCAR 21.6
CC
13
Cov
0%
_apply_from_CoreUser
20.0
insert
12.0
_role_perm_sync
12.0
CRAP 8.6
1 use utf8;
2 package Rapi::Blog::DB::Result::User;
3  
4 # Created by DBIx::Class::Schema::Loader
5 # DO NOT MODIFY THE FIRST PART OF THIS FILE
6  
7 use strict;
8 use warnings;
9  
10 use Moose;
11 use MooseX::NonMoose;
12 use MooseX::MarkAsMethods autoclean => 1;
13 extends 'DBIx::Class::Core';
14 __PACKAGE__->load_components("InflateColumn::DateTime");
15 __PACKAGE__->table("user");
16 __PACKAGE__->add_columns(
17 "id",
18 { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
19 "username",
20 { data_type => "varchar", is_nullable => 0, size => 32 },
21 "full_name",
22 {
23 data_type => "varchar",
24 default_value => \"null",
25 is_nullable => 1,
26 size => 64,
27 },
28 "image",
29 {
30 data_type => "varchar",
31 default_value => \"null",
32 is_nullable => 1,
33 size => 255,
34 },
35 "email",
36 {
37 data_type => "varchar",
38 default_value => \"null",
39 is_nullable => 1,
40 size => 255,
41 },
42 "admin",
43 { data_type => "boolean", default_value => 0, is_nullable => 0 },
44 "author",
45 { data_type => "boolean", default_value => 0, is_nullable => 0 },
46 "comment",
47 { data_type => "boolean", default_value => 1, is_nullable => 0 },
48 "disabled",
49 { data_type => "boolean", default_value => 0, is_nullable => 0 },
50 );
51 __PACKAGE__->set_primary_key("id");
52 __PACKAGE__->add_unique_constraint("full_name_unique", ["full_name"]);
53 __PACKAGE__->add_unique_constraint("username_unique", ["username"]);
54 __PACKAGE__->has_many(
55 "comments",
56 "Rapi::Blog::DB::Result::Comment",
57 { "foreign.user_id" => "self.id" },
58 { cascade_copy => 0, cascade_delete => 0 },
59 );
60 __PACKAGE__->has_many(
61 "post_authors",
62 "Rapi::Blog::DB::Result::Post",
63 { "foreign.author_id" => "self.id" },
64 { cascade_copy => 0, cascade_delete => 0 },
65 );
66 __PACKAGE__->has_many(
67 "post_creators",
68 "Rapi::Blog::DB::Result::Post",
69 { "foreign.creator_id" => "self.id" },
70 { cascade_copy => 0, cascade_delete => 0 },
71 );
72 __PACKAGE__->has_many(
73 "post_updaters",
74 "Rapi::Blog::DB::Result::Post",
75 { "foreign.updater_id" => "self.id" },
76 { cascade_copy => 0, cascade_delete => 0 },
77 );
78 __PACKAGE__->has_many(
79 "preauth_actions",
80 "Rapi::Blog::DB::Result::PreauthAction",
81 { "foreign.user_id" => "self.id" },
82 { cascade_copy => 0, cascade_delete => 0 },
83 );
84  
85  
86 # Created by DBIx::Class::Schema::Loader v0.07049 @ 2018-10-27 23:38:05
87 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:2klRAJnak1zgM9iKuGRQCQ
88  
89 use RapidApp::Util ':all';
90  
91 __PACKAGE__->load_components('+RapidApp::DBIC::Component::TableSpec');
92 __PACKAGE__->load_components('+Rapi::Blog::DB::Component::SafeResult');
93  
94 __PACKAGE__->add_virtual_columns( set_pw => {
95 data_type => "varchar",
96 is_nullable => 1,
97 sql => "SELECT NULL",
98 set_function => sub {} # This is a dummy designed to hook via AuthCore/linked_user_model
99 });
100  
101 __PACKAGE__->apply_TableSpec;
102  
103  
104 sub insert {
105 my $self = shift;
106  
107 # Fix GitHub Issue #1 - we want to do this perm check, however, if this insert is being
108 # generated via automatic inflation from the CoreSchema LinkedRow, this will result in
109 # a deep recursion (i.e. the current user *is* being inserted). For this case, we bail
110 # out of the perm check if we detect that a linkedRow sync is already in-progress
111 unless($self->{_pulling_linkedRow}) {
112 my $User = Rapi::Blog::Util->get_User;
113 die usererr "Create User: PERMISSION DENIED" if ($User && $User->id && !$User->admin);
114 }
115
116 $self->next::method(@_);
117
118 $self->_role_perm_sync;
119  
120 $self
121 }
122  
123 sub update {
124 my $self = shift;
125
126 my $User = Rapi::Blog::Util->get_User;
127
128 die usererr "Update User: PERMISSION DENIED" if (
129 $User && $User->id &&
130 !($User->admin || $self->id == $User->id)
131 );
132
133 $self->next::method(@_);
134
135 $self->_role_perm_sync;
136  
137 $self
138 }
139  
140 sub delete {
141 my $self = shift;
142
143 my $User = Rapi::Blog::Util->get_User;
144 die usererr "Delete User: PERMISSION DENIED" if ($User && $User->id && !$User->admin);
145
146 $self->next::method(@_)
147 }
148  
149  
150 # return the username if full_name is not set
151 around 'full_name' => sub {
152 my ($orig,$self,@args) = @_;
153 $self->$orig(@args) || $self->username
154 };
155  
156 sub image_url {
157 my $self = shift;
158 $self->{_image_url} //= $self->image
159 ? join('/','_ra-rel-mnt_','simplecas','fetch_content',$self->image)
160 : undef
161 }
162  
163 sub _role_perm_sync {
164 my $self = shift;
165
166 if($self->{_pulling_linkedRow}) {
167 $self->_apply_from_CoreUser($self->{_pulling_linkedRow});
168 }
169 else {
170 if($self->can('_find_linkedRow')) {
171 # This is ugly but needed to hook both sides correctly across all CRUD ops
172 my $Row = $self->_find_linkedRow || $self->_create_linkedRow;
173 $self->_apply_to_CoreUser( $Row );
174 }
175 }
176 }
177  
178  
179  
180 # change originated from CoreSchema::User:
181 sub _apply_from_CoreUser {
182 my ($self, $CoreUser) = @_;
183
184 my $cur_admin = $self->admin || 0;
185
186 $CoreUser = $CoreUser->get_from_storage if ($CoreUser->in_storage); # needed in case the username has changed
187  
188 my $LinkRs = $CoreUser->user_to_roles;
189 my $admin_cond = { username => $CoreUser->username, role => 'administrator' };
190 if($LinkRs->search_rs($admin_cond)->first) {
191 $self->admin(1);
192 }
193 else {
194 $self->admin(0);
195 }
196
197 $self->update unless ($cur_admin == $self->admin);
198 }
199  
200  
201  
202 # change originated locally:
203 sub _apply_to_CoreUser {
204 my ($self, $CoreUser) = @_;
205
206 my $LinkRs = $CoreUser->user_to_roles;
207 my $admin_cond = { username => $CoreUser->username, role => 'administrator' };
208 if($self->admin) {
209 $LinkRs->find_or_create($admin_cond);
210 }
211 else {
212 if(my $Link = $LinkRs->search_rs($admin_cond)->first) {
213 $Link->delete;
214 }
215 }
216  
217 }
218  
219  
220 sub can_post {
221 my $self = shift;
222 $self->admin || $self->author
223 }
224  
225 sub can_comment {
226 my $self = shift;
227 $self->admin || $self->comment
228 }
229  
230  
231  
232 # You can replace this text with custom code or comments, and it will be preserved on regeneration
233 __PACKAGE__->meta->make_immutable;
234 1;
235  
236 __END__