Skip to content

Commit eaa29c5

Browse files
committedJun 19, 2012
group() function Survey::ExpressionEngine that returns 1/0 depending on whether the current user is in a group

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed
 

‎docs/changelog/7.x.x.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
7.10.25
2+
- added: group() function for Survey::ExpressionEngine to test group membership
23
- added: give WebGUI::PseudoRequest a hostname method
34
- fixed: don't clobber the request handler if WebGUI::Test was loaded inside of mod_perl
45
- fixed #12365: editing a metadata may cause a fatal error (Arjan Widlak / United Knowledge)

‎lib/WebGUI/Asset/Wobject/Survey/ExpressionEngine.pm

+29
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ my $validate;
3636
my $validTargets;
3737
my $otherInstances;
3838
my $tags;
39+
my $groups;
3940

4041
=head2 value
4142
@@ -352,6 +353,24 @@ sub avg {
352353
return sum(@vals) / @vals;
353354
}
354355

356+
=head2 group ($name)
357+
358+
Utility sub that returns a boolean indicating whether the user taking the survey is a member of a specified group, by name
359+
360+
=head3 $name
361+
362+
The name of the group
363+
364+
=cut
365+
366+
sub group {
367+
my ($name) = @_;
368+
my $value = grep( $_ eq $name, @$groups ) ? 1 : 0;
369+
# warn "group($name) resolves to [$value]; groups = " . join( " ", map ">>$_<<", @$groups ) . " for userId " . $session->user->getId;
370+
$session->log->debug("group($name) resolves to [$value]; groups = @$groups");
371+
return $value;
372+
}
373+
355374
=head2 round
356375
357376
Utility sub shared with Safe compartment to allows expressions to easily round numbers
@@ -442,6 +461,15 @@ sub run {
442461
$validTargets = $opts->{validTargets};
443462
$tags = $opts->{tags} || {};
444463
$otherInstances = {};
464+
$groups = $session->db->buildArrayRef( qq{
465+
select groupName
466+
from groupings
467+
join groups using (groupId)
468+
where groupings.userId = ?
469+
and expireDate > now()
470+
}, [
471+
$session->user->getId,
472+
] );
445473

446474
if ( !$session->config->get('enableSurveyExpressionEngine') ) {
447475
$session->log->debug('enableSurveyExpressionEngine config option disabled, skipping');
@@ -469,6 +497,7 @@ sub run {
469497
$compartment->share('&restart');
470498
$compartment->share('&avg');
471499
$compartment->share('&round');
500+
$compartment->share('&group');
472501

473502
# Give them all of List::Util too
474503
$compartment->share_from( 'List::Util',

‎t/Asset/Wobject/Survey/ExpressionEngine.t

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ my $session = WebGUI::Test->session;
2222

2323
#----------------------------------------------------------------------------
2424
# Tests
25-
my $tests = 60;
25+
my $tests = 62;
2626
plan tests => $tests + 1;
2727

2828
#----------------------------------------------------------------------------
@@ -237,6 +237,13 @@ cmp_deeply( $e->run( $session, qq{jump {scoreX('$url', ext_s0) == 200} target},
237237
cmp_deeply( $e->run( $session, qq{jump {taggedX('$url', ext_tag) == 199} target}, {userId => $user->userId} ),
238238
{ jump => 'target', tags => {} }, 'external tag lookups work too' );
239239

240+
# group() utility function
241+
$session->user( { userId => 3 });
242+
cmp_deeply( $e->run( $session, qq{jump { group('Admins') } target}, ),
243+
{ jump => 'target', tags => { }, }, 'group() function recognizes us as Admins' );
244+
cmp_deeply( $e->run( $session, qq{jump { group('Space Aliens') } target}, ),
245+
{ jump => undef, tags => { }, }, 'group() function recognizes that we are not Space Aliens' );
246+
240247
# Test for nasty bugs caused by file-scoped lexicals not being properly initialised in L<ExpressionEngine::run>
241248
{
242249
# Create a second test user

0 commit comments

Comments
 (0)
Please sign in to comment.