Elive/Entity/Group.pm

filter: Statementstmt 95.4% 84 / 88 Branchbran 82.1% 23 / 28 Conditioncond 58.8% 10 / 17 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 85.0% 137 / 161 CC 23 SCAR 18.7
CC
23
Cov
88%
expand_members
13.5
stringify
6.0
BUILDARGS
3.7
CRAP 6.5
« Elive/Connection/SDK.pm ↑ Summary Elive/DAO/Singleton.pm »
1 package Elive::Entity::Group;
2 27 ▶use warnings; use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 27 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 27 extends 'Elive::Entity';
10  
11 27 ▶use Elive::Entity::Group::Members;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 27 __PACKAGE__->entity_name('Group');
14 27 __PACKAGE__->collection_name('Groups');
15  
16 27 has 'groupId' => (is => 'rw', isa => 'Str', required => 1);
17 27 __PACKAGE__->primary_key('groupId');
18  
19 27 has 'name' => (is => 'rw', isa => 'Str',
20 documentation => 'name of the group');
21 27 __PACKAGE__->_alias(groupName => 'name', freeze => 1);
22  
23 27 has 'members' => (is => 'rw', isa => 'Elive::Entity::Group::Members',
24 coerce => 1,
25 documentation => "ids of users or sub-groups");
26 27 __PACKAGE__->_alias(groupMembers => 'members', freeze => 1);
27 27 __PACKAGE__->_alias(entry => 'members');
28  
29 27 has 'domain' => (is => 'rw', isa => 'Any',
30 documentation => 'ldap domain?');
31  
32 27 has 'dn' => (is => 'rw', isa => 'Str',
33 documentation => 'LDAP Domain (where applicable)');
34  
35 sub BUILDARGS {
36 43 ▶ my $class = shift;
Subroutine BUILDARGS: called CC=3 SCAR=13.1
Pod documented
37 43 my $spec = shift;
38  
39 43 my $args;
40 43 ▶ if ($spec && ! ref $spec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec and not ref $spec) { } 0 43 43
Condition33%1/3
$spec and not ref $spec
!ll&&!rl&&r
0430
MC/DC0%0/2
$spec and not ref $spec
$spec not ref $spec
Truth table33%1/3
A and not(B)
A= $specB= not ref $spec
ABresult
0X0
100
111
41 0 my $group_id = $spec;
42 0 $group_id =~ s{^ \s* \* \s*}{}x; # just in case leading '*' leaks
43 0 $args = {groupId => $group_id};
44 }
45 else {
46 43 $args = $spec;
47 }
48  
49 43 return $args;
50 }
51  
52 coerce 'Elive::Entity::Group' => from 'HashRef|Str'
53 9 ▶ => via {Elive::Entity::Group->new($_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
54  
55 sub stringify {
56 81 ▶ my $class = shift;
Subroutine stringify: called CC=6 SCAR=18.0
Pod documented
57 81 my $data = shift;
58 81 ▶ $data ||= $class if (ref $class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 43 38 81
Condition66%2/3
$data ||= $class
l!l&&r!l&&!r
4390
MC/DC0%0/2
$data ||= $class
$data $class
Truth table66%2/3
A ||= B
A= $dataB= $class
ABresult
1X1
011
000
59  
60 81 my $prefix = '';
61  
62 81 ▶ if (Elive::Util::_reftype($data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (Elive::Util::_reftype($data) eq "HASH") 80 1 81
63 80 ▶ if (exists $data->{userId}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $data->{'userId'}) { } 2 78 80
elsif (exists $data->{'groupId'}) { } 78 0 78
64 $data = $data->{userId}
65 2 }
66 elsif (exists $data->{groupId}) {
67 78 $prefix = '*';
68 }
69 }
70  
71 81 return $prefix . $class->SUPER::stringify($data, @_);
72  
73 }
74  
75 # _restful_url()
76 # remove any leading '*'
77  
78 sub _restful_url {
79 37 ▶ my $self = shift;
Subroutine _restful_url: called CC=1 SCAR=0.0
80 37 my $connection = shift;
81 37 my $id = shift;
82 37 $id =~ s{^\*}{};
83 37 return $self->SUPER::_restful_url($connection, $id);
84 }
85  
86 =head1 NAME
87  
88 Elive::Entity::Group - Elluminate Group entity instance class
89  
90 =head1 DESCRIPTION
91  
92 Groups of users are typically used to specify session participants.
93  
94 This class is used to query and maintain groups.
95  
96 The C<members> property contains the group members as an array of user IDs
97 or sub-group objects.
98  
99 If the a site is configured for LDAP, groups are mapped to LDAP groups.
100 Group access becomes read-only. The affected methods are: C<insert>, C<update>,
101 and C<delete>.
102  
103 =cut
104  
105 sub _freeze {
106 13 ▶ my $class = shift;
Subroutine _freeze: called CC=2 SCAR=6.9
107 13 my $app_data = shift;
108  
109 13 $app_data = $class->SUPER::_freeze($app_data, @_);
110 13 ▶ $app_data->{groupId} =~ s{^\*}{} if $app_data->{groupId};
Branch: 1 branch
BranchTrueFalseTotal
if $app_data->{"groupId"} 9 4 13
111  
112 13 return $app_data;
113 }
114  
115 sub _thaw {
116 34 ▶ my $class = shift;
Subroutine _thaw: called CC=3 SCAR=11.0
117 34 my $db_data = shift;
118 34 ▶ my $path = shift || '';
Condition100%2/2
shift() || ''
l!l
286
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
119  
120 34 ▶ return $db_data unless ref $db_data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $db_data 27 7 34
121  
122 7 return $class->SUPER::_thaw($db_data, $path, @_);
123 }
124  
125 =head1 METHODS
126  
127 =cut
128  
129 =head2 insert
130  
131 #
132 # insert from an array of User Ids. Elements may be integers, strings
133 # and/or user objects.
134 #
135 my $alice = Elive::Entity::User->get_by_loginName('alice');
136 my $bob = Elive::Entity::User->get_by_loginName('bob');
137  
138 my $group = Elive::Entity::Group->insert({
139 name => 'Elluminati',
140 # following are all ok
141 members => [111111, '222222', $alice->userId, $bob ],
142 },
143 );
144  
145 #
146 # insert from a comma separated string of user IDs
147 #
148 my $group = Elive::Entity::Group->insert({
149 name => 'Elluminati',
150 members => '111111,222222,333333',
151 },
152 );
153  
154 Inserts a new group.
155  
156 =cut
157  
158 =head2 update
159  
160 $group->update({members => [111111,'222222', $alice->userId, $bob]});
161 $group->update({members => '111111,222222,333333'});
162  
163 Updates an existing group.
164  
165 =cut
166  
167 sub update {
168 2 ▶ my ($self, $data, %opt) = @_;
Subroutine update: called CC=2 SCAR=6.9
Pod documented
169 #
170 # updateGroup barfs unless the groupName is included as a parameter.
171 #
172 2 $self->set(%$data);
173 2 my @changed = $self->is_changed;
174 push (@changed, 'name')
175 2 ▶ unless grep {$_ eq 'name'} @changed;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "name";} @changed 1 1 2
176  
177 2 return $self->SUPER::update( undef, %opt, changed => \@changed);
178 }
179  
180 =head2 expand_members
181  
182 This is a utility method that includes the sum total of all members in
183 the group, including those in recursively nested sub-groups. The list
184 is further reduced to only include unique members.
185  
186 my @all_members = $group_obj->expand_members();
187  
188 It returns C<loginNames> as an array of strings.
189  
190 =cut
191  
192 sub expand_members {
193 5 ▶ my $self = shift;
Subroutine expand_members: called CC=12 SCAR=26.0
Pod documented
194 5 my %seen = @_;
195  
196 5 my @members;
197  
198 5 ▶ foreach (@{ $self->members || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$self->members or [] 0 5 5
199 24 my @elements;
200  
201 24 ▶ if (Scalar::Util::blessed($_)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_) and $_->can('groupId') and $_->can('expand_members')) { } 5 19 24
Condition66%4/6
builtin::blessed($_) and $_->can("groupId") and $_->can("expand_members")
!ll&&!rl&&r
1905
builtin::blessed($_) and $_->can("groupId")
!ll&&!rl&&r
1905
MC/DC33%1/3
builtin::blessed($_) and $_->can("groupId") and $_->can("expand_members")
builtin::blessed($_) $_->can("groupId") $_->can("expand_members")
Truth table50%2/4
A and B and C
A= builtin::blessed($_)B= $_->can("groupId")C= $_->can("expand_members")
ABCresult
0XX0
10X0
1100
1111
202 && $_->can('groupId')
203 && $_->can('expand_members')) {
204 # recursive expansion
205 @elements = $_->expand_members(%seen)
206 5 ▶ unless $seen{g => $_->groupId}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{join $;, "g", $_->groupId}++ 3 2 5
207 }
208 else {
209 19 @elements = (Elive::Util::string($_));
210 }
211  
212 24 foreach my $member (@elements) {
213 24 ▶ $member = Elive::Util::string($member)
Branch: 1 branch
BranchTrueFalseTotal
if ref $member 0 24 24
214 if ref $member;
215  
216 24 ▶ if (defined $member && $member =~ m{^\*}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $member and $member =~ /^\*/) { } 0 24 24
Condition33%1/3
defined $member and $member =~ /^\*/
!ll&&!rl&&r
0240
MC/DC0%0/2
defined $member and $member =~ /^\*/
defined $member $member =~ /^\*/
Truth table33%1/3
A and B
A= defined $memberB= $member =~ /^\*/
ABresult
0X0
100
111
217 # should be caught in BUILDARGS anyway...
218 0 carp "skipping unexpanded group: $member";
219 }
220 else {
221 push(@members, $member)
222 24 ▶ unless $seen{u => $member}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{join $;, "u", $member}++ 17 7 24
223 }
224 }
225 }
226  
227 5 @members = sort @members;
228 5 return @members;
229 }
230  
231 27 1;
« Elive/Connection/SDK.pm ↑ Summary Elive/DAO/Singleton.pm »