WE/DB.pm

filter: Statementstmt 34.8% 31 / 89 Branchbran 15.7% 6 / 38 Conditioncond 15.3% 2 / 13 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 33.3% 6 / 18 Podpod 92.3% 12 / 13 totaltotal 32.4% 58 / 179 CC 29 SCAR 25.1
CC
29
Cov
28%
new
20.0
init
20.0
delete_db_contents
20.0
CRAP 12.3
« WE_Content/Tools.pm ↑ Summary WE/DB/ComplexUser/AuthPOP3.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: DB.pm,v 1.14 2005/02/03 00:06:26 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::DB;
18  
19 =head1 NAME
20  
21 WE::DB - root of web editor database collection
22  
23 =head1 SYNOPSIS
24  
25 $root = new WE::DB
26  
27 =head1 DESCRIPTION
28  
29 Instantiate a new root for a web.editor database. This class will
30 usually be overwritten by a class doing the dirt work of opening the
31 sub-databases. See L<WE_Singlesite::Root> for an example.
32  
33 =cut
34  
35 14 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 14 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 14 $VERSION = sprintf("%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/);
40  
41 14 ▶use vars qw(@DBs);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 14 @DBs = qw(UserDB ObjDB ContentDB LinkDB OnlineUserDB NameDB);
43  
44 14 __PACKAGE__->mk_accessors(qw/CurrentUser CurrentGroups/, @DBs);
45  
46 =head2 CONSTRUCTOR new([%args])
47  
48 If C<-class =E<gt> Classname> is specified, then a specialized class
49 constructor will be used (for example C<WE_Sample::Root>). Additional
50 arguments are passed to this constructor.
51  
52 =cut
53  
54 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
55 0 my($class, %args) = @_;
56 0 ▶ if ($args{-class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'-class'}) { } 0 0 0
57 0 my $class = delete $args{-class};
58 0 eval "require $class";
59 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
60 0 my $err = $@;
61 0 ▶ if (!$class->can("new")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("new")) 0 0 0
62 # There's no constructor: it is really a failure...
63 0 die $@;
64 }
65 # else the class is probably a file-less class already defined
66 }
67 0 $class->new(%args);
68 } else {
69 0 my $self = {};
70 0 bless $self, $class;
71 }
72 }
73  
74 =head2 METHODS
75  
76 =over 4
77  
78 =item use_databases(@classes)
79  
80 Tell the framework to L<use|perlfunc/"use Module"> the given database
81 classes. These classes will be automatically loaded.
82  
83 The classes can be specified as abbreviated names (without a ":" in
84 the class name), in this case C<WE::DB> is automatically prepended.
85  
86 The special class name C<:all> represents the classes C<Obj>, C<User>
87 and C<Content>. Note that these are not really all available databases
88 in a typical WE system.
89  
90 =cut
91  
92 15 ▶sub use_databases {
Subroutine use_databases: called CC=7 SCAR=19.7
Pod documented
93 15 my($self, @classes) = @_;
94 TRY_CLASS:
95 15 foreach my $class (@classes) {
96 # allow abbreviations
97 32 ▶ if (#$class !~ /^WE::DB/ && # XXX remove if harmless
Branch: 1 branch
BranchTrueFalseTotal
unless ($class =~ /:/) 32 0 32
98 $class !~ /:/) {
99 32 $class = "WE::DB::$class";
100 }
101 32 ▶ if ($class eq ':all') { # XXX this is not "all"
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq ":all") 0 32 32
102 0 push @classes, qw(Obj User Content);
103 0 next TRY_CLASS;
104 }
105  
106 {
107 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
108 # stolen from base.pm, has_version:
109 32 my $vglob = ${$class.'::'}{VERSION};
110 32 ▶ if ($vglob && *$vglob{SCALAR}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vglob and *$vglob{"SCALAR"}) 6 26 32
Condition100%2/2
$vglob and *$vglob{"SCALAR"}
!ll
266
MC/DC100%1/1
$vglob and *$vglob{"SCALAR"}
$vglob
Truth table100%2/2
A
A= $vglob
Aresult
00
11
111 # class is already loaded
112 6 next TRY_CLASS;
113 }
114 }
115  
116 26 eval "require $class; $class->import";
117 26 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 14 12 26
118 }
119 }
120  
121 =item login($user, $password)
122  
123 Identify the user and do a login to the system by putting him to the
124 OnlineUser database. Return true if everything was right.
125  
126 =cut
127  
128 0 ▶sub login {
Subroutine login: not called CC=3 SCAR=24.8
Pod documented
129 0 my($self, $user, $password) = @_;
130 0 my $r = $self->identify($user, $password);
131 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
132 0 ▶ $self->OnlineUserDB->login($user) if $self->OnlineUserDB;
Branch: 1 branch
BranchTrueFalseTotal
if $self->OnlineUserDB 0 0 0
133 }
134 0 $r;
135 }
136  
137 =item logout($user)
138  
139 Logout the user.
140  
141 =cut
142  
143 0 ▶sub logout {
Subroutine logout: not called CC=2 SCAR=17.9
Pod documented
144 0 my($self, $user) = @_;
145 0 ▶ $self->OnlineUserDB->logout($user) if $self->OnlineUserDB;
Branch: 1 branch
BranchTrueFalseTotal
if $self->OnlineUserDB 0 0 0
146 }
147  
148 =item identify($user, $password)
149  
150 Identify $user with $password and return true if the authentification
151 is successful. Also set the CurrentUser member. This does not make any
152 changes to the OnlineUser database.
153  
154 =cut
155  
156 0 ▶sub identify {
Subroutine identify: not called CC=2 SCAR=17.9
Pod documented
157 0 my($self, $user, $password) = @_;
158 0 my $r = $self->UserDB->identify($user, $password);
159 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
160 0 $self->CurrentUser($user);
161 }
162 0 $r;
163 }
164  
165 =item is_allowed($action, $object_id)
166  
167 Return a true value if the current user is allowed to do C<$action> on
168 object C<$object_id>.
169  
170 This method should be overridden, because it provides no access
171 control in this form.
172  
173 =cut
174  
175 0 ▶sub is_allowed {
Subroutine is_allowed: not called CC=2 SCAR=17.9
Pod documented
176 0 my($self, $action, $obj_id) = @_;
177 0 my $user = $self->CurrentUser;
178 0 ▶ return 0 if !$user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 0 0
179 0 warn "The is_allowed() method in WE::DB should be overridden!\n";
180 0 return 1;
181 }
182  
183 =item is_releasable_page($obj)
184  
185 Return true if the given object is releasable. The default
186 implementation always returns true.
187  
188 =cut
189  
190 0 ▶sub is_releasable_page {
Subroutine is_releasable_page: not called CC=1 SCAR=6.9
Pod documented
191 0 my($self, $obj) = @_;
192 0 return 1;
193 }
194  
195 =item root_object
196  
197 Return the root object of the underlying object database
198  
199 =cut
200  
201 0 ▶sub root_object {
Subroutine root_object: not called CC=1 SCAR=6.9
Pod documented
202 0 my($self) = @_;
203 0 $self->ObjDB->root_object;
204 }
205  
206 =item init
207  
208 Initialize the underlying databases.
209  
210 =cut
211  
212 0 ▶sub init {
Subroutine init: not called CC=4 SCAR=30.0
Pod documented
213 0 my($self) = @_;
214  
215 0 foreach my $db (@DBs) {
216 0 ▶ if (defined $self->{$db} && $self->{$db}->can("init")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$db} and $self->{$db}->can("init")) 0 0 0
Condition0%0/3
defined $self->{$db} and $self->{$db}->can("init")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{$db} and $self->{$db}->can("init")
defined $self->{$db} $self->{$db}->can("init")
Truth table0%0/3
A and B
A= defined $self->{$db}B= $self->{$db}->can("init")
ABresult
0X0
100
111
217 0 $self->{$db}->init;
218 }
219 }
220 }
221  
222 =item delete_db_contents
223  
224 Delete the contents from B<all> underlying databases.
225  
226 =cut
227  
228 0 ▶sub delete_db_contents {
Subroutine delete_db_contents: not called CC=4 SCAR=30.0
Pod documented
229 0 my($self) = @_;
230  
231 0 foreach my $db (@DBs) {
232 0 ▶ if (exists $self->{$db} && $self->{$db}->can("delete_db_contents")) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$db} and $self->{$db}->can("delete_db_contents")) 0 0 0
Condition0%0/3
exists $self->{$db} and $self->{$db}->can("delete_db_contents")
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{$db} and $self->{$db}->can("delete_db_contents")
exists $self->{$db} $self->{$db}->can("delete_db_contents")
Truth table0%0/3
A and B
A= exists $self->{$db}B= $self->{$db}->can("delete_db_contents")
ABresult
0X0
100
111
233 0 $self->{$db}->delete_db_contents;
234 }
235 }
236 }
237  
238 =item delete_db
239  
240 Delete B<all> underlying databases. This will also remove the files,
241 not just the contents as in C<delete_db_contents>.
242  
243 =cut
244  
245 0 ▶sub delete_db {
Subroutine delete_db: not called CC=4 SCAR=30.0
Pod documented
246 0 my $self = shift;
247 0 foreach my $db (@DBs) {
248 0 ▶ if (exists $self->{$db} && $self->{$db}->can("delete_db")) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$db} and $self->{$db}->can("delete_db")) 0 0 0
Condition0%0/3
exists $self->{$db} and $self->{$db}->can("delete_db")
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{$db} and $self->{$db}->can("delete_db")
exists $self->{$db} $self->{$db}->can("delete_db")
Truth table0%0/3
A and B
A= exists $self->{$db}B= $self->{$db}->can("delete_db")
ABresult
0X0
100
111
249 0 $self->{$db}->delete_db;
250 }
251 }
252 }
253  
254 =item CurrentUser
255  
256 Set or get the currently logged in user.
257  
258 =cut
259  
260 0 ▶sub CurrentUser {
Subroutine CurrentUser: not called CC=3 SCAR=24.8
Pod documented
261 0 my $self = shift;
262 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
263 0 $self->{CurrentUser} = $_[0];
264 0 ▶ if ($self->UserDB) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->UserDB) 0 0 0
265 0 $self->CurrentGroups([ $self->UserDB->get_groups($self->{CurrentUser}) ]);
266 }
267 }
268 0 $self->{CurrentUser};
269 }
270  
271 0 ▶sub CurrentLang {
Subroutine CurrentLang: not called CC=4 SCAR=30.0
Pod undocumented
272 0 my $self = shift;
273 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
274 0 $self->{CurrentLang} = $_[0];
275 } else {
276 0 ▶ if ($self->CurrentUser && 0 #$self->CurrentUser->Lang
Branch: 1 branch
BranchTrueFalseTotal
if ($self->CurrentUser and 0) { } 0 0 0
Condition0%0/2
$self->CurrentUser and 0
!ll
00
MC/DC0%0/1
$self->CurrentUser and 0
$self->CurrentUser
Truth table0%0/2
A
A= $self->CurrentUser
Aresult
00
11
277 ) {
278 0 $self->CurrentUser->Lang;
279 } else {
280 0 $self->{CurrentLang};
281 }
282 }
283 }
284  
285 14 1;
286  
287 __END__
« WE_Content/Tools.pm ↑ Summary WE/DB/ComplexUser/AuthPOP3.pm »