Changeset 1555

Show
Ignore:
Timestamp:
03/24/08 08:08:33 (8 months ago)
Author:
fumiakiy
Message:

Changed the way to handle many blog ids. Instead of putting everything in a cookie, it special cases if the user is a superuser, or if the user has assigned to more than 20 blogs. BugId:67996

Also added invalidation of commenter_url cookie value when logging out.

Also added a check to see if commenter is a TypeKey user, and redirect appropriately.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/enzo/default_templates/javascript.mtml

    r1118 r1555  
    2727</MTIfNonEmpty> 
    2828 
     29var AUTHOR = 1; 
     30var COMMENTER = 2; 
    2931var commenter_name; 
    30 var commenter_blog_ids; 
     32var commenter_status; 
     33var commenter_id; 
     34var commenter_url 
    3135var is_preview; 
    3236var mtcmtmail; 
     
    4549<MTIfRegistrationAllowed> 
    4650    <MTIfRegistrationRequired> 
    47     if ( commenter_name && 
    48          ( !commenter_id 
    49         || commenter_blog_ids.indexOf("'<$MTBlogID$>'") > -1)) 
     51    if ( commenter_status == COMMENTER ) 
    5052    { 
    5153        hideDocumentElement('comment-form-name'); 
     
    6062    <MTElse> 
    6163    // comments are allowed but registration not required 
    62     if ( commenter_name && 
    63          ( !commenter_id 
    64         || commenter_blog_ids.indexOf("'<$MTBlogID$>'") > -1)) 
     64    if ( commenter_status == COMMENTER ) 
    6565    { 
    6666        hideDocumentElement('comment-form-name'); 
     
    9898function writeCommenterGreeting(commenter_name, entry_id, blog_id, commenter_id, commenter_url) { 
    9999<MTIfRegistrationAllowed> 
    100     if ( commenter_name && 
    101          ( !commenter_id 
    102         || commenter_blog_ids.indexOf("'" + blog_id + "'") > -1)) 
    103     { 
    104         var url; 
    105         if (commenter_id) { 
    106             url = '<$MTCGIPath$><$MTCommentScript$>?__mode=edit_profile&commenter=' + commenter_id + '&blog_id=' + blog_id; 
    107             if (entry_id) { 
    108                 url += '&entry_id=' + entry_id; 
     100    if ( commenter_status > 0 ) { 
     101        var commenter_link; 
     102        if ( commenter_status == COMMENTER ) { 
     103            if (commenter_url) { 
     104                commenter_link = '<a href="' + commenter_url + '">' + commenter_name + '</a>'; 
    109105            } else { 
    110                 url += '&static=1'
     106                commenter_link = commenter_name
    111107            } 
    112         } else if (commenter_url) { 
    113             url = commenter_url; 
    114         } else { 
    115             url = null; 
     108        } else if ( commenter_status == AUTHOR ) { 
     109            if (commenter_id) { 
     110                commenter_link = '<a href="<$MTCGIPath$><$MTCommentScript$>?__mode=edit_profile&commenter=' + commenter_id + '&blog_id=' + blog_id; 
     111                if (entry_id) { 
     112                    commenter_link += '&entry_id=' + entry_id; 
     113                } else { 
     114                    commenter_link += '&static=1'; 
     115                } 
     116                commenter_link += '">' + commenter_name + '</a>'; 
     117            } 
     118 
    116119        } 
    117         var content = '<__trans phrase="Thanks for signing in," escape="js"> '; 
    118         if (url) { 
    119             content += '<a href="' + url + '">' + commenter_name + '</a>'; 
    120         } else { 
    121             content += commenter_name; 
    122         } 
    123         content += '<__trans phrase=". Now you can comment." escape="js"> (<a href="<$MTRemoteSignOutLink static="1"$>&entry_id=' + entry_id + '"><__trans phrase="sign out" escape="js"></a>)'; 
    124         document.write(content); 
     120        document.write( 
     121            '<__trans phrase="Thanks for signing in, [_1]. Now you can comment. ([_2]sign out[_3])" params="' + commenter_link + '%%<a href="<$MTRemoteSignOutLink static="1"$>&entry_id=' + entry_id + '">%%</a>">' 
     122        ); 
    125123    } else if (commenter_name) { 
    126             document.write('<__trans phrase="You do not have permission to comment on this blog." escape="js"> (<a href="<$MTRemoteSignOutLink static="1"$>&entry_id=' + entry_id + '"><__trans phrase="sign out" escape="js"></a>)'); 
     124        document.write('<__trans phrase="You do not have permission to comment on this blog. ([_1]sign out[_2])" params="<a href="<$MTRemoteSignOutLink static="1"$>&entry_id=' + entry_id + '">%%</a>">'); 
    127125    } else { 
    128126<MTIfRegistrationRequired> 
    129         document.write('<a href="<$MTCGIPath$><$MTCommentScript$>?__mode=login&entry_id=' + entry_id + '&blog_id=' + blog_id + '&static=1"><__trans phrase="Sign in" escape="js">' + '</a>' + '<__trans phrase=" to comment on this entry." escape="js">')
     127        var phrase = '<__trans phrase="[_1]Sign in[_2] to comment on this entry." params="<a href="<$MTCGIPath$><$MTCommentScript$>?__mode=login&entry_id=' + entry_id + '&blog_id=' + blog_id + '&static=1">%%</a>">'
    130128<MTElse> 
    131         document.write('<a href="<$MTCGIPath$><$MTCommentScript$>?__mode=login&entry_id=' + entry_id + '&blog_id=' + blog_id + '&static=1"><__trans phrase="Sign in" escape="js">' + '</a>' + '<__trans phrase=" to comment on this entry," escape="js"> <__trans phrase="or " escape="js"><a href="javascript:void(0);" onclick="showAnonymousForm();"><__trans phrase="comment anonymously." escape="js"></a>')
     129        var phrase = '<__trans phrase="[_1]Sign in[_2] to comment on this entry, or [_3]comment anonymously[_2]." params="<a href="<$MTCGIPath$><$MTCommentScript$>?__mode=login&entry_id=' + entry_id + '&blog_id=' + blog_id + '&static=1">%%</a>%%<a href="javascript:void(0);" onclick="showAnonymousForm();">">'
    132130</MTIfRegistrationRequired> 
     131        document.write(phrase); 
    133132    } 
    134133</MTIfRegistrationAllowed> 
     
    140139} else { 
    141140    commenter_name = getCookie('commenter_name'); 
     141    commenter_url = getCookie('commenter_url'); 
    142142    ids = getCookie('commenter_id').split(':'); 
    143143    commenter_id = ids[0]; 
    144     commenter_blog_ids = ids[1]; 
    145     commenter_url = getCookie('commenter_url'); 
     144    if ( ids[1] == 'S' ) { 
     145        commenter_status = AUTHOR; 
     146    } 
     147    else if ( ids[1] == 'N' ) { 
     148        document.write('<script src="<$MTCGIPath$><$MTCommentScript$>?__mode=cmtr_status_js&blog_id=<$MTBlogID$>"></script>'); 
     149    } 
     150    else if ( commenter_name && !commenter_id ) { 
     151        commenter_status = COMMENTER; 
     152    } 
     153    else if ( commenter_name  
     154      && commenter_id 
     155      && ( ids[1].indexOf("'<$MTBlogID$>'") > -1 ) ) { 
     156        commenter_status = AUTHOR; 
     157    } 
     158    else { 
     159        commenter_status = 0; 
     160    } 
    146161} 
    147162</MTIfRegistrationAllowed> 
  • branches/enzo/lib/MT/App.pm

    r1550 r1555  
    10011001    $app->bake_cookie(%name_kookee); 
    10021002    if (defined $id) { 
    1003         my @blogs; 
     1003        my $blog_ids; 
    10041004        if ($app->user && $app->user->is_superuser) { 
    1005             @blogs = $app->model('blog')->load( undef, { 
    1006                 fetchonly => [ 'id' ], 
    1007             }); 
     1005            # Do not send blog ids in cookie because it may become huge. 
     1006            $blog_ids = 'S'; 
    10081007        } 
    10091008        else { 
    1010             @blogs = $app->model('blog')->load(undef, 
     1009            my @blogs = $app->model('blog')->load(undef, 
    10111010              { 
    10121011                fetchonly => [ 'id' ], 
     
    10201019              } 
    10211020            ); 
    1022         } 
    1023         my $blog_ids = @blogs ? "'" . join("','", map { $_->id } @blogs) . "'" : ''; 
    1024  
    1025         my $perm = MT::Permission->load({ blog_id => $blog_id, author_id => $id }); 
    1026         if ($perm) { 
    1027             # double-check to see if this user hasn't been denied commenting 
    1028             # permission. user has 'comment' permission through a role, 
    1029             # but check for a restriction to comment on this blog 
    1030             if ($perm->is_restricted('comment')) { 
    1031                 $blog_ids =~ s/(,|^)'$blog_id'(,|$)//; 
    1032             } 
    1033  
    1034             # But if the permission carries a 'can administer' permission 
    1035             # they should be allowed 
    1036             if ($blog_id && ($blog_ids !~ m/(,|^)'$blog_id'(,|$)/)) { 
    1037                 if ($perm->can_administer_blog()) { 
    1038                     # user is a blog administrator, so yes, they can comment too 
    1039                     $blog_ids .= ($blog_ids ne '' ? ',' : '') 
    1040                         . "'" . $blog_id . "'"; 
     1021 
     1022            # Has permissions to 20+ blogs - do not send these ids in cookie. 
     1023            $blog_ids = 20 < scalar(@blogs) 
     1024              ? 'N' 
     1025              : @blogs 
     1026                ? "'" . join("','", map { $_->id } @blogs) . "'"  
     1027                : ''; 
     1028        } 
     1029 
     1030        if ( $blog_ids ne 'S' && $blog_ids ne 'N' ) { 
     1031            my $perm = MT::Permission->load({ blog_id => $blog_id, author_id => $id }); 
     1032            if ($perm) { 
     1033                # double-check to see if this user hasn't been denied commenting 
     1034                # permission. user has 'comment' permission through a role, 
     1035                # but check for a restriction to comment on this blog 
     1036                if ($perm->is_restricted('comment')) { 
     1037                    $blog_ids =~ s/(,|^)'$blog_id'(,|$)//; 
    10411038                } 
    1042             } 
    1043         } 
    1044         else { 
    1045             if ($blog_id && ($blog_ids !~ m/(,|^)'$blog_id'(,|$)/)) { 
    1046                 # extra check to see if this user can comment on requested 
    1047                 # blog; this is specific to the Comment application, so 
    1048                 # only do this if we're running the comments app. 
    1049                 if ( $app->isa( 'MT::App::Comments' )) { 
    1050                     if ( $app->_check_commenter_author($app->user, $blog_id) ) { 
    1051                         # is this blog open to commenting from registered users? 
    1052                         # if so, this user really can comment, even though they 
    1053                         # don't have explicit permissions for it 
     1039 
     1040                # But if the permission carries a 'can administer' permission 
     1041                # they should be allowed 
     1042                if ($blog_id && ($blog_ids !~ m/(,|^)'$blog_id'(,|$)/)) { 
     1043                    if ($perm->can_administer_blog()) { 
     1044                        # user is a blog administrator, so yes, they can comment too 
    10541045                        $blog_ids .= ($blog_ids ne '' ? ',' : '') 
    10551046                            . "'" . $blog_id . "'"; 
     1047                    } 
     1048                } 
     1049            } 
     1050            else { 
     1051                if ($blog_id && ($blog_ids !~ m/(,|^)'$blog_id'(,|$)/)) { 
     1052                    # extra check to see if this user can comment on requested 
     1053                    # blog; this is specific to the Comment application, so 
     1054                    # only do this if we're running the comments app. 
     1055                    if ( $app->isa( 'MT::App::Comments' )) { 
     1056                        if ( $app->_check_commenter_author($app->user, $blog_id) ) { 
     1057                            # is this blog open to commenting from registered users? 
     1058                            # if so, this user really can comment, even though they 
     1059                            # don't have explicit permissions for it 
     1060                            $blog_ids .= ($blog_ids ne '' ? ',' : '') 
     1061                                . "'" . $blog_id . "'"; 
     1062                        } 
    10561063                    } 
    10571064                } 
     
    11041111                  -expires => "+${timeout}s"); 
    11051112    $app->bake_cookie(%kookee); 
     1113    my %url_kookee = (-name => 'commenter_url', 
     1114                       -value => '', 
     1115                       -path => '/', 
     1116                       -expires => "+${timeout}s"); 
     1117    $app->bake_cookie(%url_kookee); 
    11061118    my %name_kookee = (-name => 'commenter_name', 
    11071119                       -value => '', 
  • branches/enzo/lib/MT/App/Comments.pm

    r1550 r1555  
    3535        handle_sign_in   => \&handle_sign_in, 
    3636        cmtr_name_js     => \&commenter_name_js, 
     37        cmtr_status_js   => \&commenter_status_js, 
    3738        edit_profile     => \&edit_commenter_profile, 
    3839        save_profile     => \&save_commenter_profile, 
     
    13721373        my %cookies = $app->cookies(); 
    13731374        $app->_invalidate_commenter_session( \%cookies ); 
     1375        if ( $commenter && ( 'TypeKey' ne $commenter->auth_type ) ) { 
     1376            # Remove logout parameter so MT does not go to TypeKey 
     1377            # when unnecessary. 
     1378            $app->param( 'logout', 0 ); 
     1379        } 
    13741380        $result = 1; 
    13751381    } 
     
    14301436} 
    14311437 
     1438sub _commenter_status { 
     1439    my $app = shift; 
     1440    my ( $commenter_id ) = @_; 
     1441    my $blog_id          = $app->param('blog_id') || 0; 
     1442    my $commenter_status = '0'; 
     1443    my $user = $app->model('author')->load($commenter_id); 
     1444    if ($user && $user->is_superuser) { 
     1445        $commenter_status = 'AUTHOR'; 
     1446    } 
     1447    else { 
     1448        # FIXME: this may be incomplete since the user 
     1449        # may in fact be able to comment on other blogs; 
     1450        # they just haven't signed into them yet 
     1451        my $perm = MT::Permission->load( 
     1452              { 
     1453                blog_id     => $blog_id, 
     1454                permissions => { like => "\%'comment'\%" }, 
     1455                author_id   => $commenter_id 
     1456              } 
     1457            ); 
     1458        if ( $perm ) { 
     1459            if ( $perm->is_restricted('comment') 
     1460              && !$perm->can_administer_blog() ) { 
     1461                $commenter_status = '0'; 
     1462            } 
     1463            else { 
     1464                $commenter_status = 'AUTHOR'; 
     1465            } 
     1466        } 
     1467        elsif ( MT::Author::COMMENTER() == $user->type ) { 
     1468            $commenter_status = 'COMMENTER'; 
     1469        } 
     1470        elsif ( $app->_check_commenter_author($user, $blog_id) ) { 
     1471            $commenter_status = 'AUTHOR'; 
     1472        } 
     1473    } 
     1474    $commenter_status; 
     1475} 
     1476 
     1477sub commenter_status_js { 
     1478    local $SIG{__WARN__} = sub { }; 
     1479    my $app     = shift; 
     1480    my $ids     = $app->cookie_val('commenter_id') || q(); 
     1481 
     1482    my $commenter_id; 
     1483    if ($ids) { 
     1484        my @ids = split ':', $ids; 
     1485        $commenter_id    = $ids[0]; 
     1486    } 
     1487 
     1488    my $commenter_status = '0'; 
     1489    if ($commenter_id) { 
     1490        $commenter_status = $app->_commenter_status( $commenter_id ); 
     1491    } 
     1492    $commenter_status = encode_js( $commenter_status ); 
     1493    return <<JS; 
     1494commenter_status = $commenter_status; 
     1495JS 
     1496} 
     1497 
    14321498sub commenter_name_js { 
    14331499    local $SIG{__WARN__} = sub { }; 
     
    14361502    my $ids            = $app->cookie_val('commenter_id') || q(); 
    14371503    my $commenter_url  = $app->cookie_val('commenter_url') || q(); 
    1438     my $blog_id        = $app->param('blog_id') || 0; 
    14391504 
    14401505    my $commenter_id; 
     
    14501515    $app->set_header( 'Expires'       => '-1' ); 
    14511516 
    1452     my $blog_ids = q()
     1517    my $commenter_status = '0'
    14531518    if ($commenter_id) { 
    1454         my @blogs; 
    1455         my $user = $app->model('author')->load($commenter_id); 
    1456         if ($user && $user->is_superuser) { 
    1457             @blogs = $app->model('blog')->load( undef, { fetchonly => [ 'id' ] } ); 
    1458         } 
    1459         else { 
    1460             # FIXME: this may be incomplete since the user 
    1461             # may in fact be able to comment on other blogs; 
    1462             # they just haven't signed into them yet 
    1463             @blogs = $app->model('blog')->load(undef, 
    1464               { fetchonly => [ 'id' ], 
    1465                 join => MT::Permission->join_on('blog_id', 
    1466                   { 
    1467                     permissions => { like => "\%'comment'\%" }, 
    1468                     author_id   => $commenter_id 
    1469                   } 
    1470                 ) 
    1471               } 
    1472             ); 
    1473         } 
    1474         $blog_ids = @blogs ? "'" . join("','", map { $_->id } @blogs) . "'" : ''; 
    1475  
    1476         my $perm = MT::Permission->load({ blog_id => $blog_id, author_id => $commenter_id }); 
    1477         if ($perm) { 
    1478             # double-check to see if this user hasn't been denied commenting 
    1479             # permission. user has 'comment' permission through a role, 
    1480             # but check for a restriction to comment on this blog 
    1481             if ($perm->is_restricted('comment')) { 
    1482                 $blog_ids =~ s/(,|^)'$blog_id'(,|$)//; 
    1483             } 
    1484  
    1485             # But if the permission carries a 'can administer' permission 
    1486             # they should be allowed 
    1487             if ($blog_id && ($blog_ids !~ m/(,|^)'$blog_id'(,|$)/)) { 
    1488                 if ($perm->can_administer_blog()) { 
    1489                     # user is a blog administrator, so yes, they can comment too 
    1490                     $blog_ids .= ($blog_ids ne '' ? ',' : '') 
    1491                         . "'" . $blog_id . "'"; 
    1492                 } 
    1493             } 
    1494         } 
    1495         else { 
    1496             if ($blog_id && ($blog_ids !~ m/(,|^)'$blog_id'(,|$)/)) { 
    1497                 # extra check to see if this user can comment on requested 
    1498                 # blog 
    1499                 if ( $app->_check_commenter_author($user, $blog_id) ) { 
    1500                     # is this blog open to commenting from registered users? 
    1501                     # if so, this user really can comment, even though they 
    1502                     # don't have explicit permissions for it 
    1503                     $blog_ids .= ($blog_ids ne '' ? ',' : '') 
    1504                         . "'" . $blog_id . "'"; 
    1505                 } 
    1506             } 
    1507         } 
    1508     } 
    1509     $commenter_name = encode_js( $commenter_name ); 
    1510     $commenter_url  = encode_js( $commenter_url ); 
    1511     $commenter_id   = encode_js( $commenter_id ); 
     1519        $commenter_status = $app->_commenter_status( $commenter_id ); 
     1520    } 
     1521    elsif ($commenter_name) { 
     1522        $commenter_status = 'COMMENTER'; 
     1523    } 
     1524    $commenter_name   = encode_js( $commenter_name ); 
     1525    $commenter_url    = encode_js( $commenter_url ); 
     1526    $commenter_id     = encode_js( $commenter_id ); 
     1527    $commenter_status = encode_js( $commenter_status ); 
    15121528    return <<JS; 
    15131529commenter_name = '$commenter_name'; 
    15141530commenter_id = '$commenter_id'; 
    15151531commenter_url = '$commenter_url'; 
    1516 commenter_blog_ids = "$blog_ids"
     1532commenter_status = $commenter_status
    15171533JS 
    15181534}