[dw-free] Make the can_* edge methods return error messages and add can_leave
[commit: http://hg.dwscoalition.org/dw-free/rev/09c25acf76de]
http://bugs.dwscoalition.org/show_bug.cgi?id=699
can_* error messages and add can_leave
Patch by
janinedog.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=699
can_* error messages and add can_leave
Patch by
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Files modified:
- bin/upgrading/en.dat
- cgi-bin/DW/User/Edges/CommMembership.pm
- cgi-bin/DW/User/Edges/WatchTrust.pm
- htdocs/community/leave.bml
- htdocs/community/leave.bml.text
-------------------------------------------------------------------------------- diff -r e73818cfb31a -r 09c25acf76de bin/upgrading/en.dat --- a/bin/upgrading/en.dat Mon Apr 06 16:24:11 2009 +0000 +++ b/bin/upgrading/en.dat Mon Apr 06 17:00:14 2009 +0000 @@ -807,6 +807,40 @@ date.month.september.long=September date.month.september.long=September date.month.september.short=Sep + +edges.join.error.targetbanneduser=You have been banned from the community. + +edges.join.error.targetnotcommunity=The account being joined must be a community. + +edges.join.error.targetnotopen=The community is closed to new members. + +edges.join.error.targetnotvisible=The community must be visible. + +edges.join.error.usernotpersonal=Your account must be a personal account. + +edges.join.error.usernotvisible=Your account must be visible. + +edges.join.error.userunderage=You must be 18 or older to join this community. + +edges.leave.error.lastmaintainer=You are not allowed to leave this community because you are the sole maintainer. If you wish to leave the community, please appoint another maintainer and then try leaving again. + +edges.trust.error.targetinvalidstatusvis=The person who you are granting access to must not be purged, suspended, or locked. + +edges.trust.error.targetnotindividual=The account that you are granting access to must be a personal or identity account. + +edges.trust.error.userbannedtarget=The person who you are granting access to has been banned by you. + +edges.trust.error.userequalstarget=You cannot grant yourself access. + +edges.trust.error.usernotindividual=Your account must be a personal or identity account. + +edges.trust.error.usernotvisible=Your account must be visible. + +edges.watch.error.targetinvalidstatusvis=The person who you are subscribing to must not be purged, suspended, or locked. + +edges.watch.error.usernotindividual=Your account must be a personal or identity account. + +edges.watch.error.usernotvisible=Your account must be visible. email=Email diff -r e73818cfb31a -r 09c25acf76de cgi-bin/DW/User/Edges/CommMembership.pm --- a/cgi-bin/DW/User/Edges/CommMembership.pm Mon Apr 06 16:24:11 2009 +0000 +++ b/cgi-bin/DW/User/Edges/CommMembership.pm Mon Apr 06 17:00:14 2009 +0000 @@ -125,34 +125,57 @@ sub member_userids { # to the target. note: if you don't pass a target user, then we return # a generic 1/0 meaning "this account is allowed to have a member edge". sub can_join { - my ( $u, $tu ) = @_; + my ( $u, $tu, %opts ) = @_; $u = LJ::want_user( $u ) or confess 'invalid user object'; $tu = LJ::want_user( $tu ); + + my $errref = $opts{errref}; # if the user is a maintainer, skip every other check return 1 if $tu && $u->can_manage( $tu ); # the user must be a personal account - return 0 unless $u->is_personal; + unless ( $u->is_personal ) { + $$errref = LJ::Lang::ml( 'edges.join.error.usernotpersonal' ); + return 0; + } # the user must be visible - return 0 unless $u->is_visible; + unless ( $u->is_visible ) { + $$errref = LJ::Lang::ml( 'edges.join.error.usernotvisible' ); + return 0; + } if ( $tu ) { # the target must be a community - return 0 unless $tu->is_community; + unless ( $tu->is_community ) { + $$errref = LJ::Lang::ml( 'edges.join.error.targetnotcommunity' ); + return 0; + } # the target must be visible - return 0 unless $tu->is_visible; + unless ( $tu->is_visible ) { + $$errref = LJ::Lang::ml( 'edges.join.error.targetnotvisible' ); + return 0; + } # the target must not have banned the user - return 0 if $tu->has_banned( $u ); + if ( $tu->has_banned( $u ) ) { + $$errref = LJ::Lang::ml( 'edges.join.error.targetbanneduser' ); + return 0; + } # make sure the user isn't underage and trying to join an adult community - return 0 unless $u->can_join_adult_comm( comm => $tu ); + unless ( $u->can_join_adult_comm( comm => $tu ) ) { + $$errref = LJ::Lang::ml( 'edges.join.error.userunderage' ); + return 0; + } # the community must be open membership - return 0 unless $tu->is_open_membership; + unless ( $tu->is_open_membership ) { + $$errref = LJ::Lang::ml( 'edges.join.error.targetnotopen' ); + return 0; + } } # okay, good to go! @@ -161,4 +184,32 @@ sub can_join { *LJ::User::can_join = \&can_join; +# returns 1/0 depending on if the source is allowed to remove a member edge +# from the target. note: if you don't pass a target user, then we return +# a generic 1/0 meaning "this account is allowed to not have a member edge". +sub can_leave { + my ( $u, $tu, %opts ) = @_; + $u = LJ::want_user( $u ) or confess 'invalid user object'; + $tu = LJ::want_user( $tu ); + + my $errref = $opts{errref}; + + # if the user is the last maintainer, they can't leave + if ( $tu ) { + my @maintids = $tu->maintainer_userids; + my $ismaint = grep { $_ == $u->id } @maintids; + my $othermaints = grep { $_ != $u->id } @maintids; + + if ( $ismaint && !$othermaints ) { + $$errref = LJ::Lang::ml( 'edges.leave.error.lastmaintainer' ); + return 0; + } + } + + # okay, good to go! + return 1; +} +*LJ::User::can_leave = \&can_leave; + + 1; diff -r e73818cfb31a -r 09c25acf76de cgi-bin/DW/User/Edges/WatchTrust.pm --- a/cgi-bin/DW/User/Edges/WatchTrust.pm Mon Apr 06 16:24:11 2009 +0000 +++ b/cgi-bin/DW/User/Edges/WatchTrust.pm Mon Apr 06 17:00:14 2009 +0000 @@ -947,28 +947,48 @@ sub trust_group_contains { # to the target. note: if you don't pass a target user, then we return # a generic 1/0 meaning "this account is allowed to have a trust edge". sub can_trust { - my ( $u, $tu ) = @_; + my ( $u, $tu, %opts ) = @_; $u = LJ::want_user( $u ) or confess 'invalid user object'; $tu = LJ::want_user( $tu ); + my $errref = $opts{errref}; + # the user must be an individual - return 0 unless $u->is_individual; + unless ( $u->is_individual ) { + $$errref = LJ::Lang::ml( 'edges.trust.error.usernotindividual' ); + return 0; + } # the user must be visible - return 0 unless $u->is_visible; + unless ( $u->is_visible ) { + $$errref = LJ::Lang::ml( 'edges.trust.error.usernotvisible' ); + return 0; + } if ( $tu ) { # the user cannot be the same as the target - return 0 if $u->equals( $tu ); + if ( $u->equals( $tu ) ) { + $$errref = LJ::Lang::ml( 'edges.trust.error.userequalstarget' ); + return 0; + } # the target must be an individual - return 0 unless $tu->is_individual; + unless ( $tu->is_individual ) { + $$errref = LJ::Lang::ml( 'edges.trust.error.targetnotindividual' ); + return 0; + } # the target must not be purged/suspended/locked - return 0 if $tu->is_expunged || $tu->is_suspended || $tu->is_locked; + if ( $tu->is_expunged || $tu->is_suspended || $tu->is_locked ) { + $$errref = LJ::Lang::ml( 'edges.trust.error.targetinvalidstatusvis' ); + return 0; + } # the target must not be banned by the user - return 0 if $u->has_banned( $tu ); + if ( $u->has_banned( $tu ) ) { + $$errref = LJ::Lang::ml( 'edges.trust.error.userbannedtarget' ); + return 0; + } } # okay, good to go! @@ -981,19 +1001,30 @@ sub can_trust { # to the target. note: if you don't pass a target user, then we return # a generic 1/0 meaning "this account is allowed to have a watch edge". sub can_watch { - my ( $u, $tu ) = @_; + my ( $u, $tu, %opts ) = @_; $u = LJ::want_user( $u ) or confess 'invalid user object'; $tu = LJ::want_user( $tu ); + my $errref = $opts{errref}; + # the user must be an individual - return 0 unless $u->is_individual; + unless ( $u->is_individual ) { + $$errref = LJ::Lang::ml( 'edges.watch.error.usernotindividual' ); + return 0; + } # the user must be visible - return 0 unless $u->is_visible; + unless ( $u->is_visible ) { + $$errref = LJ::Lang::ml( 'edges.watch.error.usernotvisible' ); + return 0; + } if ( $tu ) { # the target must not be purged/suspended/locked - return 0 if $tu->is_expunged || $tu->is_suspended || $tu->is_locked; + if ( $tu->is_expunged || $tu->is_suspended || $tu->is_locked ) { + $$errref = LJ::Lang::ml( 'edges.watch.error.targetinvalidstatusvis' ); + return 0; + } } # okay, good to go! diff -r e73818cfb31a -r 09c25acf76de htdocs/community/leave.bml --- a/htdocs/community/leave.bml Mon Apr 06 16:24:11 2009 +0000 +++ b/htdocs/community/leave.bml Mon Apr 06 17:00:14 2009 +0000 @@ -35,20 +35,13 @@ return "<?h1 $ML{'Error'} h1?><?p $ML{'error.invalidform'} p?>" unless LJ::check_form_auth(); - # get current list of maintainers to make sure the last one doesn't leave - my $maintids = LJ::load_rel_user($cu->{userid}, 'A'); - return $error->($ML{'error.nodb'}) unless ref $maintids eq 'ARRAY'; + my $error_msg; + return $error->( $error_msg ) + unless $remote->can_leave( $cu, errref => \$error_msg ); - # error if we're a maintainer and there are no others - my $ismaint = grep { $_ == $remote->{userid} } @$maintids; - if ($ismaint) { - my $othermaints = grep { $_ != $remote->{userid} } @$maintids; - - return $error->($ML{'.label.lastmaintainer'}) unless $othermaints; - - # log maint removal to userlog - $cu->log_event('maintainer_remove', { actiontarget => $remote->id, remote => $remote }); - } + # log to userlog if remote is a maintainer + $cu->log_event( 'maintainer_remove', { actiontarget => $remote->id, remote => $remote } ) + if $remote->can_manage( $cu ); # remove user's membership to community if ($watching && !$memberof) { diff -r e73818cfb31a -r 09c25acf76de htdocs/community/leave.bml.text --- a/htdocs/community/leave.bml.text Mon Apr 06 16:24:11 2009 +0000 +++ b/htdocs/community/leave.bml.text Mon Apr 06 17:00:14 2009 +0000 @@ -8,8 +8,6 @@ .label.buttontostopwatch=Press the button below to stop watching the "[[commname]]" community. .label.infoerror=The specified community information is not valid. - -.label.lastmaintainer=You are not allowed to leave this community because you are the sole maintainer. If you wish to leave the community, please appoint another maintainer and then try leaving again. .label.logoutfirst2=To leave a community you must first <a [[aopts]]>log in</a>. --------------------------------------------------------------------------------