[dw-free] Error handling for WTF backend
[commit: http://hg.dwscoalition.org/dw-free/rev/817d2372b6fa]
http://bugs.dwscoalition.org/show_bug.cgi?id=368
Add generic can_trust/can_watch methods to determine if someone is allowed
to watch/trust someone else or in general.
Patch by
mark.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=368
Add generic can_trust/can_watch methods to determine if someone is allowed
to watch/trust someone else or in general.
Patch by
![[staff profile]](https://www.dreamwidth.org/img/silk/identity/user_staff.png)
Files modified:
- bin/test/test-wtf
- cgi-bin/DW/User/Edges/WatchTrust.pm
-------------------------------------------------------------------------------- diff -r bf8396ff3697 -r 817d2372b6fa bin/test/test-wtf --- a/bin/test/test-wtf Fri Mar 06 08:15:49 2009 +0000 +++ b/bin/test/test-wtf Fri Mar 06 08:28:34 2009 +0000 @@ -442,6 +442,18 @@ push @tests, [ 'add edge again, test mas } ]; ################################################################################ +push @tests, [ 'allowed to watch and trust', sub +{ + return ( $u1->can_watch && $u2->can_trust ) ? 1 : 0; +} ]; + +################################################################################ +push @tests, [ 'allowed to watch and trust the other', sub +{ + return ( $u1->can_watch( $u2 ) && $u2->can_trust( $u2 ) ) ? 1 : 0; +} ]; + +################################################################################ $| = 1; my $id = 1; foreach my $test ( @tests ) { diff -r bf8396ff3697 -r 817d2372b6fa cgi-bin/DW/User/Edges/WatchTrust.pm --- a/cgi-bin/DW/User/Edges/WatchTrust.pm Fri Mar 06 08:15:49 2009 +0000 +++ b/cgi-bin/DW/User/Edges/WatchTrust.pm Fri Mar 06 08:28:34 2009 +0000 @@ -851,6 +851,49 @@ sub trust_group_contains { *LJ::User::trust_group_contains = \&trust_group_contains; +# returns 1/0 depending on if the source is allowed to add a trust edge +# 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 ) = @_; + $u = LJ::want_user( $u ) or confess 'invalid user object'; + $tu = LJ::want_user( $tu ); + + # only individuals are allowed to trust eachother + return 0 if ! $u->is_individual || ( $tu && ! $tu->is_individual ); + + # both must be visible + return 0 if ! $u->is_visible || ( $tu && ! $tu->is_visible ); + + # that was simple... + return 1; +} +*LJ::User::can_trust = \&can_trust; + + +# returns 1/0 depending on if the source is allowed to add a watch edge +# 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 ) = @_; + $u = LJ::want_user( $u ) or confess 'invalid user object'; + $tu = LJ::want_user( $tu ); + + # only individuals are allowed to watch + return 0 unless $u->is_individual; + + # both must be visible + return 0 if ! $u->is_visible || ( $tu && ! $tu->is_visible ); + + # and you're not allowed to watch identity accounts (they can't post) + return 0 if $tu && $tu->is_identity; + + # that was kinda simple... + return 1; +} +*LJ::User::can_watch = \&can_watch; + + --------------------------------------------------------------------------------