vadvs (vadvs) wrote in changelog,
vadvs
vadvs
changelog

[ljcom] r9638: LJGI-95: add json is a one of response f...

Committer: vsukhanov
LJGI-95: add json is a one of response format.
U   trunk/htdocs/gadgets/login.bml
Modified: trunk/htdocs/gadgets/login.bml
===================================================================
--- trunk/htdocs/gadgets/login.bml	2010-10-21 08:26:38 UTC (rev 9637)
+++ trunk/htdocs/gadgets/login.bml	2010-10-21 11:48:26 UTC (rev 9638)
@@ -11,40 +11,48 @@
 #
     use strict;
     use XML::Simple;
+    use LJ::JSON;
     use vars qw(%GET %POST);
 
     BML::set_content_type("text/xml; charset=utf-8");
     BML::noparse();
     BML::finish();
 
+    my $serialize = $GET{j} 
+        ? sub { LJ::JSON->to_json(shift) } 
+        : sub { XMLout(shift, 
+                        RootName => shift, 
+                        XMLDecl => '<?xml version="1.0" encoding="utf-8" ?>'
+                        ) };
+
     my $ok_answer = sub {
-        my $user = shift;
-
-        return XMLout(
+        my $u = shift;
+        my $type = 'L';
+        if (my $ident = $u->identity){
+            $type = $ident->typeid;
+        }
+        return $serialize->(
             {
-                user =>
-                    [
-                        $user
-                    ]
+                user => ($GET{j} ? $u->username : [$u->username]),
+                type => $type,
+                loged => 1,
+                is_cyr => LJ::SUP->is_sup_enabled($u) ? 1 : 0,
             },
-            RootName => 'success', XMLDecl => '<?xml version="1.0" encoding="utf-8" ?>' );
+            'success');
     };
 
     my $err_answer = sub {
         my $reason = shift;
 
-        return XMLout(
+        return $serialize->(
                 {
-                    reason =>
-                        [
-                            $reason
-                        ]
+                    reason => ($GET{j} ? $reason : [$reason])
                 },
-                RootName => 'error', XMLDecl => '<?xml version="1.0" encoding="utf-8" ?>' );
+                'error');
     };
 
     my $u = LJ::get_remote();
-    return $ok_answer->($u->user) if $u && LJ::isu($u);
+    return $ok_answer->($u) if $u && LJ::isu($u);
 
     return $err_answer->('wrong parameters') unless LJ::did_post();
 
@@ -61,5 +69,5 @@
 
     $u->make_login_session($POST{remember} ? 'long' : 'short');
 
-    return $ok_answer->($u->user);
+    return $ok_answer->($u);
 _code?>

Tags: bml, ljcom, vadvs
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 1 comment