Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added seesion_key and session state data to the JSON output. #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ Here is a list of known issues and things this plugin has not been tested with:

* `status` - Either "1" if successful, or "0" otherwise. If 0, then "message" will contain helpful debug information to indicate what might have happened.
* `message` - A simple text message returned by the system to indicate what happened as a result of trying to login.
* `session_key` - The session id of the new login session.
* `user` - Session state data, same info that get stored in default MT cookie:
* `sid` - session id
* `name` - display name
* `url` - user URL
* `email - user's email
* `userpic` - url to user's userpic image
* `is_authenticated` - boolean
* `is_author` - boolean
* `is_trusted` - boolean
* `is_anonymous` - boolean
* `can_post` - boolean
* `can_comment` - boolean
* `is_banned` - boolean


## Status Messages

Expand Down
66 changes: 63 additions & 3 deletions plugins/AJAXLogin/lib/AJAXLogin/Plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ sub ajax_login {
}
MT::Auth->new_login( $app, $commenter );
if ( $app->_check_commenter_author( $commenter, $blog_id ) ) {
$app->make_commenter_session($commenter);
my $session_key = $app->make_commenter_session($commenter);
my $session_state = session_state($session_key, $commenter, $blog, $blog_id);
return _send_json_response( $app,
{ status => 1, message => "session created" } );
{ status => 1, message => "session created",
session_key => $session_key,
user => $session_state } );

#return $app->redirect_to_target;
}
Expand Down Expand Up @@ -112,6 +115,63 @@ sub ajax_login {

}

#modified from MT::App::session_state
sub session_state {
my ( $session_key, $commenter, $blog, $blog_id ) = @_;
my $c;

if ( $session_key && $commenter ) {
$c = {
sid => $session_key,
name => $commenter->nickname || '(Display Name not set)',
url => $commenter->url,
email => $commenter->email,
userpic => scalar $commenter->userpic_url,
profile => "", # profile link url
is_authenticated => 1,
is_author =>
( $commenter->type == MT::Author::AUTHOR() ? 1 : 0 ),
is_trusted => 0,
is_anonymous => 0,
can_post => 0,
can_comment => 0,
is_banned => 0,
};
if ( $blog_id && $blog ) {
my $blog_perms = $commenter->blog_perm($blog_id);
my $banned = $commenter->is_banned($blog_id) ? 1 : 0;
$banned = 0 if $blog_perms && $blog_perms->can_administer;
$banned ||= 1 if $commenter->status == MT::Author::BANNED();
$c->{is_banned} = $banned;

# FIXME: These may not be accurate in 'SingleCommunity' mode...
my $can_comment = $banned ? 0 : 1;
$can_comment = 0
unless $blog->allow_unreg_comments
|| $blog->allow_reg_comments;
$c->{can_comment} = $can_comment;
$c->{can_post}
= ( $blog_perms && $blog_perms->can_create_post ) ? 1 : 0;
$c->{is_trusted} =
( $commenter->is_trusted($blog_id) ? 1 : 0 ),
}
}

unless ($c) {
my $can_comment = $blog && $blog->allow_anon_comments ? 1 : 0;
$c = {
is_authenticated => 0,
is_trusted => 0,
is_anonymous => 1,
can_post => 0, # no anonymous posts
can_comment => $can_comment,
is_banned => 0,
};
}

return $c;
}

sub _send_json_response {
my ( $app, $result ) = @_;
require JSON;
Expand All @@ -123,4 +183,4 @@ sub _send_json_response {
}

1;
__END__
__END__