WE_Singlesite/Root.pm

filter: Statementstmt 8.9% 15 / 168 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 16.0% 4 / 25 Podpod 80.9% 17 / 21 totaltotal 10.6% 36 / 338 CC 58 SCAR 58.0
CC
58
Cov
5%
new
870.0
export_db
72.0
is_allowed
72.0
CRAP 330.9
« WebEditor/OldController/Admin.pm ↑ Summary Apache/AuthenWE.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: Root.pm,v 1.24 2005/02/03 00:06:30 eserte Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2001 Online Office Berlin. All rights reserved.
8 # Copyright (C) 2002 Slaven Rezic.
9 # This is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License, see the file COPYING.
11  
12 #
13 # Mail: slaven@rezic.de
14 # WWW: http://we-framework.sourceforge.net
15 #
16  
17 package WE_Singlesite::Root;
18  
19 =head1 NAME
20  
21 WE_Singlesite::Root - a simple implementation for a site
22  
23 =head1 SYNOPSIS
24  
25 $root = new WE_Singlesite::Root -rootdir => $root_directory_for_database;
26  
27 =head1 DESCRIPTION
28  
29 A simple instantiation for C<WE::DB>.
30  
31 =head1 ADDITIONAL MEMBERS
32  
33 =over
34  
35 =item RootDir
36  
37 The root directory for all databases.
38  
39 =back
40  
41 =head1 OVERRIDEABLE METHODS
42  
43 To change the default classes for the subdatabases, override the
44 following methods to return another class string:
45  
46 =over 4
47  
48 =item ObjDBClass
49  
50 By default L<WE::DB::Obj>
51  
52 =item UserDBClass
53  
54 By default L<WE::DB::User>
55  
56 =item ContentDBClass
57  
58 By default L<WE::DB::Content>
59  
60 =item OnlineUserDBClass
61  
62 By default L<WE::DB::OnlineUser>
63  
64 =item NameDBClass
65  
66 By default L<WE::DB::Name>
67  
68 =back
69  
70 To change the default file names for the subdatabases, override the
71 following methods to return another filename (just the basename):
72  
73 =over
74  
75 =item ObjDBFile
76  
77 By default F<objdb.db>
78  
79 =item UserDBFile
80  
81 By default F<userdb.db>
82  
83 =item ContentDBFile
84  
85 By default F<content>
86  
87 =item OnlineUserDBFile
88  
89 By default F<onlinedb.db>
90  
91 =item NameDBFile
92  
93 By default F<name.db>
94  
95 =back
96  
97 To change other aspects of the subdatabases, change the following
98 methods (B<WARNING>: The semantics of the following two may
99 change!!!):
100  
101 =over
102  
103 =item SerializerClass
104  
105 By default Data::Dumper
106  
107 =item DBClass
108  
109 By default DB_File
110  
111 =back
112  
113 =head1 METHODS
114  
115 =over 4
116  
117 =cut
118  
119 13 ▶use base qw(WE::DB);
Subroutine BEGIN: called CC=1 SCAR=0.0
120 # Unfortunately old projects rely on this "use":
121 13 ▶use WE::Obj;
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 13 __PACKAGE__->mk_accessors(qw/RootDir/);
124  
125 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 13 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
127 13 $VERSION = sprintf("%d.%02d", q$Revision: 1.24 $ =~ /(\d+)\.(\d+)/);
128  
129 0 ▶sub new {
Subroutine new: not called CC=29 SCAR=67.7
Pod documented
130 0 my($class, %args) = @_;
131 0 my $self = {};
132 0 bless $self, $class;
133  
134 0 my $db_dir = delete $args{-rootdir};
135 0 ▶ die "No -rootdir given" if !defined $db_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $db_dir 0 0 0
136 0 $self->RootDir($db_dir);
137 0 ▶ my $readonly = defined $args{-readonly} ? delete $args{-readonly} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-readonly'} ? : 0 0 0
138 0 ▶ my $autocreate = delete $args{-autocreate} || 0;
Condition0%0/2
delete $args{'-autocreate'} || 0
l!l
00
MC/DC0%0/1
delete $args{'-autocreate'} || 0
delete $args{'-autocreate'}
Truth table0%0/2
A
A= delete $args{'-autocreate'}
Aresult
11
00
139 0 ▶ if (!-d $db_dir && $autocreate) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -d $db_dir and $autocreate) 0 0 0
Condition0%0/3
not -d $db_dir and $autocreate
!ll&&!rl&&r
000
MC/DC0%0/2
not -d $db_dir and $autocreate
not -d $db_dir $autocreate
Truth table0%0/3
not(A) and B
A= not -d $db_dirB= $autocreate
ABresult
0X0
100
111
140 0 require File::Path;
141 0 File::Path::mkpath($db_dir);
142 }
143 0 ▶ if (!$readonly && !-w $db_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($readonly or -w $db_dir) 0 0 0
Condition0%0/3
$readonly or -w $db_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
$readonly or -w $db_dir
$readonly -w $db_dir
Truth table0%0/3
A or B
A= $readonlyB= -w $db_dir
ABresult
1X1
011
000
144 0 die "The -rootdir $db_dir is not writable (needed for lockfiles etc.)";
145 }
146 0 my $connect = $args{-connect};
147 0 ▶ my $writeonly = defined $args{-writeonly} ? delete $args{-writeonly} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-writeonly'} ? : 0 0 0
148 0 ▶ my $locking = defined $args{-locking} ? delete $args{-locking} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-locking'} ? : 0 0 0
149 0 ▶ my $serializer = defined $args{-serializer} ? delete $args{-serializer} : $self->SerializerClass;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-serializer'} ? : 0 0 0
150 0 ▶ my $failsafe = defined $args{-failsafe} ? delete $args{-failsafe} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-failsafe'} ? : 0 0 0
151 0 ▶ my $db = defined $args{-db} ? delete $args{-db} : $self->DBClass;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-db'} ? : 0 0 0
152 0 ▶ my $cache = defined $args{-cache} ? delete $args{-cache} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-cache'} ? : 0 0 0
153  
154 0 ▶ if ($self->ObjDBClass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ObjDBClass) 0 0 0
155 0 $self->use_databases($self->ObjDBClass);
156 0 eval {
157 0 $self->ObjDB
158 ($self->ObjDBClass->new($self, "$db_dir/" . $self->ObjDBFile,
159 -connect => $connect,
160 -readonly => $readonly,
161 -writeonly => $writeonly,
162 -locking => $locking,
163 -serializer => $serializer,
164 -db => $db,
165 -cache => $cache,
166 ));
167  
168 0 WE::Obj->use_classes(':all');
169 };
170 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
171 0 ▶ $failsafe ? warn $@ : die $@;
Branch: 1 branch
BranchTrueFalseTotal
$failsafe ? : 0 0 0
172 }
173 }
174  
175 0 ▶ if ($self->UserDBClass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->UserDBClass) 0 0 0
176 0 $self->use_databases($self->UserDBClass);
177 0 eval {
178 0 $self->UserDB
179 ($self->UserDBClass->new($self, "$db_dir/" . $self->UserDBFile,
180 -connect => $connect,
181 -readonly => $readonly,
182 -writeonly => $writeonly,
183 ));
184 };
185 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
186 0 ▶ $failsafe ? warn $@ : die $@;
Branch: 1 branch
BranchTrueFalseTotal
$failsafe ? : 0 0 0
187 }
188 }
189  
190 0 ▶ if ($self->ContentDBClass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ContentDBClass) 0 0 0
191 0 $self->use_databases($self->ContentDBClass);
192 0 eval {
193 0 $self->ContentDB
194 ($self->ContentDBClass->new($self, "$db_dir/" . $self->ContentDBFile,
195 -connect => $connect,
196 -readonly => $readonly,
197 -writeonly => $writeonly,
198 ));
199 };
200 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
201 0 ▶ $failsafe ? warn $@ : die $@;
Branch: 1 branch
BranchTrueFalseTotal
$failsafe ? : 0 0 0
202 }
203 }
204  
205 0 ▶ if ($self->OnlineUserDBClass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->OnlineUserDBClass) 0 0 0
206 0 $self->use_databases($self->OnlineUserDBClass);
207 0 eval {
208 0 $self->OnlineUserDB
209 ($self->OnlineUserDBClass->new($self, "$db_dir/" . $self->OnlineUserDBFile,
210 -connect => $connect,
211 -readonly => $readonly,
212 -writeonly => $writeonly,
213 ));
214 };
215 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
216 0 ▶ $failsafe ? warn $@ : die $@;
Branch: 1 branch
BranchTrueFalseTotal
$failsafe ? : 0 0 0
217 }
218 }
219  
220 0 ▶ if ($self->NameDBClass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->NameDBClass) 0 0 0
221 0 $self->use_databases($self->NameDBClass);
222 0 eval {
223 0 $self->NameDB
224 ($self->NameDBClass->new($self, "$db_dir/" . $self->NameDBFile,
225 -connect => $connect,
226 -readonly => $readonly,
227 -writeonly => $writeonly,
228 ));
229 };
230 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
231 0 ▶ $failsafe ? warn $@ : die $@;
Branch: 1 branch
BranchTrueFalseTotal
$failsafe ? : 0 0 0
232 }
233 }
234  
235 0 $self;
236 }
237  
238 0 ▶sub ObjDBClass { "WE::DB::Obj" }
Subroutine ObjDBClass: not called CC=1 SCAR=6.9
Pod documented
239 0 ▶sub UserDBClass { "WE::DB::User" }
Subroutine UserDBClass: not called CC=1 SCAR=6.9
Pod documented
240 0 ▶sub ContentDBClass { "WE::DB::Content" }
Subroutine ContentDBClass: not called CC=1 SCAR=6.9
Pod documented
241 0 ▶sub OnlineUserDBClass { "WE::DB::OnlineUser" }
Subroutine OnlineUserDBClass: not called CC=1 SCAR=6.9
Pod documented
242 0 ▶sub NameDBClass { "WE::DB::Name" }
Subroutine NameDBClass: not called CC=1 SCAR=6.9
Pod documented
243  
244 0 ▶sub ObjDBFile { "objdb.db" }
Subroutine ObjDBFile: not called CC=1 SCAR=6.9
Pod documented
245 0 ▶sub UserDBFile { "userdb.db" }
Subroutine UserDBFile: not called CC=1 SCAR=6.9
Pod documented
246 0 ▶sub ContentDBFile { "content" }
Subroutine ContentDBFile: not called CC=1 SCAR=6.9
Pod documented
247 0 ▶sub OnlineUserDBFile { "onlinedb.db" }
Subroutine OnlineUserDBFile: not called CC=1 SCAR=6.9
Pod documented
248 0 ▶sub NameDBFile { "name.db" }
Subroutine NameDBFile: not called CC=1 SCAR=6.9
Pod documented
249  
250 0 ▶sub SerializerClass { "Data::Dumper" }
Subroutine SerializerClass: not called CC=1 SCAR=6.9
Pod documented
251 0 ▶sub DBClass { "DB_File" }
Subroutine DBClass: not called CC=1 SCAR=6.9
Pod documented
252  
253 0 ▶sub disconnect {
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod undocumented
254 0 my($self) = @_;
255 0 $self->ObjDB->disconnect;
256 0 $self->UserDB->disconnect;
257 # $self->ContentDB->disconnect;
258 0 $self->OnlineUserDB->disconnect;
259 0 $self->NameDB->disconnect;
260 }
261  
262 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
263 0 my $self = shift;
264 0 $self->SUPER::init(@_);
265 0 my $u = $self->UserDB;
266 }
267  
268 0 ▶sub export_db {
Subroutine export_db: not called CC=8 SCAR=42.8
Pod undocumented
269 0 my($self, %args) = @_;
270 0 ▶ $args{-as} = 'perl' if !exists $args{-as};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"-as"} 0 0 0
271 0 ▶ $args{-db} = [qw(ObjDB UserDB OnlineUserDB NameDB)] if !exists $args{-db};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"-db"} 0 0 0
272 0 ▶ if ($args{-as} eq 'perl') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'-as'} eq 'perl') { } 0 0 0
273 0 my @obj;
274 0 my @varnames = @{$args{-db}};
275 0 foreach my $db (@{$args{-db}}) {
276 0 ▶ my $db_obj = eval '$self->'.$db; die $@ if $@; # for 5.005
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
277 0 ▶ if ($db_obj->can('Connected') && !$db_obj->Connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($db_obj->can("Connected") and not $db_obj->Connected) 0 0 0
Condition0%0/3
$db_obj->can("Connected") and not $db_obj->Connected
!ll&&!rl&&r
000
MC/DC0%0/2
$db_obj->can("Connected") and not $db_obj->Connected
$db_obj->can("Connected") not $db_obj->Connected
Truth table0%0/3
A and not(B)
A= $db_obj->can("Connected")B= not $db_obj->Connected
ABresult
0X0
100
111
278 0 die "The export_db method requires a permanent connection to the database $db";
279 }
280 0 push @obj, $db_obj->{DB};
281 }
282 0 require Data::Dumper;
283 0 my $dd = Data::Dumper->new([@obj], [@varnames]);
284 0 $dd->Indent(0); # for Windows
285 0 return $dd->Dump;
286 } else {
287 0 die "Export type $args{-as} is not implemented";
288 }
289 }
290  
291 0 ▶sub import_db {
Subroutine import_db: not called CC=6 SCAR=37.4
Pod undocumented
292 0 my($self, %args) = @_;
293 0 ▶ $args{-as} = 'perl' if !exists $args{-as};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"-as"} 0 0 0
294 0 ▶ die "No -string option given" if !defined $args{-string};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"-string"} 0 0 0
295 0 require Safe;
296 0 my $pkg = __PACKAGE__ . '::Safe';
297 0 my $cpt = Safe->new($pkg);
298 0 $cpt->reval($args{-string});
299 0 foreach my $db (qw(ObjDB UserDB OnlineUserDB NameDB)) {
300 0 ▶ my $db_obj = eval '$self->'.$db; die $@ if $@; # for 5.005
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
301 0 my $o = eval "\$${pkg}::$db";
302 0 ▶ if (defined $o) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $o) 0 0 0
303 0 %{$db_obj->{DB}} = %$o;
304 }
305 }
306 }
307  
308 0 ▶sub get_permissions {
Subroutine get_permissions: not called CC=3 SCAR=24.8
Pod undocumented
309 0 my($self) = @_;
310 0 require WE::Util::Permissions;
311 0 my $new_location = $self->RootDir . "/../etc/permissions";
312 0 my $permfile = $self->RootDir . "/permissions";
313 0 ▶ if (-e $permfile) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $permfile) { } 0 0 0
elsif (-e $new_location) { } 0 0 0
314 0 warn "Detected permissions file at old location. Please consider to move the file to $new_location";
315 } elsif (-e $new_location) {
316 0 $permfile = $new_location;
317 } else {
318 0 warn "Cannot find permissions file in $new_location";
319 }
320 0 my $perm = WE::Util::Permissions->new(-file => $permfile);
321 0 $self->{Permissions} = $perm;
322 }
323  
324 =item is_allowed($action, $object_id)
325  
326 Return a true value if the current user is allowed to do C<$action> on
327 object C<$object_id>.
328  
329 Currently are these actions defined:
330  
331 =over 4
332  
333 =item release
334  
335 The user is allowed to release a document ("freigeben").
336  
337 =item publish
338  
339 The user is allowed to publish a site.
340  
341 =item change-folder
342  
343 The user is allowed to do folder manipulation, that is, he is allowed
344 to add or delete folders.
345  
346 =item change-doc
347  
348 The user is allowed to do document manipulation, that is, he is
349 allowed to add, edit or delete documents.
350  
351 =back
352  
353 If there is no current user, then always a false value is returned.
354  
355 =cut
356  
357 0 ▶sub is_allowed {
Subroutine is_allowed: not called CC=8 SCAR=42.8
Pod documented
358 0 my($self, $action, $obj_id) = @_;
359 0 my $user = $self->CurrentUser;
360 0 ▶ return 0 if !$user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 0 0
361 0 my $permissions = $self->{Permissions};
362 0 ▶ if (!$permissions) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($permissions) 0 0 0
363 0 $permissions = $self->get_permissions;
364 }
365 0 my $path;
366 0 ▶ if (defined $obj_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj_id) 0 0 0
367 0 $path = $self->ObjDB->pathname($obj_id);
368 }
369 0 my $group = $self->CurrentGroups;
370 0 ▶ $permissions->is_allowed
Branch: 2 branches
BranchTrueFalseTotal
$group && ref $group && @$group > 0 ? : 0 0 0
defined $path ? : 0 0 0
Condition0%0/6
$group and ref $group and @$group > 0
!ll&&!rl&&r
000
$group and ref $group
!ll&&!rl&&r
000
MC/DC0%0/3
$group and ref $group and @$group > 0
$group ref $group @$group > 0
Truth table0%0/4
A and B and C
A= $groupB= ref $groupC= @$group > 0
ABCresult
0XX0
10X0
1100
1111
371 (-user => $user,
372 ($group && ref $group && @$group > 0 ? (-group => $group) : ()),
373 (defined $path ? (-page => $path) : ()),
374 -process => $action,
375 );
376 }
377  
378 0 ▶sub is_releasable_page {
Subroutine is_releasable_page: not called CC=3 SCAR=24.8
Pod documented
379 0 my($self, $obj) = @_;
380 0 my $objdb = $self->ObjDB;
381 0 $objdb->objectify_params($obj);
382 0 ▶ my $release_state = $obj->Release_State || "";
Condition0%0/2
$obj->Release_State || ''
l!l
00
MC/DC0%0/1
$obj->Release_State || ''
$obj->Release_State
Truth table0%0/2
A
A= $obj->Release_State
Aresult
11
00
383 0 ▶ return 0 if $release_state eq 'inactive';
Branch: 1 branch
BranchTrueFalseTotal
if $release_state eq "inactive" 0 0 0
384 0 return 1;
385 }
386  
387 =item release_page($obj, %args)
388  
389 Release the page with object $obj. Pass the object, not the id. If the
390 value of the C<-useversioning> argument is true, then do a check-in of
391 the released objects (see also C<useversioning> in C<WEprojectinfo>).
392 The arguments C<Title>, C<VisibleToMenu> and C<Rights> are used, if
393 defined, to set the respective object members.
394  
395 =cut
396  
397 0 ▶sub release_page {
Subroutine release_page: not called CC=7 SCAR=40.3
Pod documented
398 0 my $self = shift;
399 0 my $newobj = shift;
400 0 my %args = @_;
401 0 ▶ if (!defined $newobj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $newobj) 0 0 0
402 0 die "Object is missing in release_page";
403 }
404 0 my $objdb = $self->ObjDB;
405 0 my $useversioning = delete $args{-useversioning};
406 0 my $pid = $newobj->Id;
407  
408 0 my $versionedobj;
409 0 ▶ if ($useversioning) {
Branch: 1 branch
BranchTrueFalseTotal
if ($useversioning) 0 0 0
410 0 $versionedobj = $objdb->ci($pid);
411 0 $versionedobj->{Release_State} = "released";
412 # XXX have to re-get the object because ci changed it!
413 0 $newobj = $objdb->get_object($pid);
414 }
415 0 $newobj->{Release_State} = "released";
416  
417 0 foreach my $copykey (qw(Title VisibleToMenu Rights)) {
418 0 ▶ if (defined $args{$copykey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$copykey}) 0 0 0
419 0 ▶ $versionedobj->{$copykey} = $args{$copykey} if $versionedobj;
Branch: 1 branch
BranchTrueFalseTotal
if $versionedobj 0 0 0
420 0 $newobj->{$copykey} = $args{$copykey};
421 }
422 }
423 0 ▶ $objdb->replace_object($versionedobj) if $versionedobj;
Branch: 1 branch
BranchTrueFalseTotal
if $versionedobj 0 0 0
424 0 $objdb->replace_object($newobj);
425  
426 0 $newobj;
427 }
428  
429 13 1;
430  
431 __END__
« WebEditor/OldController/Admin.pm ↑ Summary Apache/AuthenWE.pm »