Last active
August 29, 2015 13:57
-
-
Save MohamedBassem/9790545 to your computer and use it in GitHub Desktop.
Symfony's backend session sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
megasoft_entangle_homepage: | |
pattern: /hello/{name} | |
defaults: { _controller: MegasoftEntangleBundle:Default:index } | |
create: | |
pattern: /create | |
defaults: { _controller: MegasoftEntangleBundle:Session:create } | |
methods: [POST] | |
whoami: | |
pattern: /whoami/{sessionId} | |
defaults: { _controller: MegasoftEntangleBundle:Session:whoami } | |
methods: [GET] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Megasoft\EntangleBundle\Controller; | |
use Megasoft\EntangleBundle\Entity\User; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpFoundation\Request; | |
class SessionController extends Controller | |
{ | |
private function generate($len){ | |
$ret = ''; | |
$seed = "abcdefghijklmnopqrstuvwxyz123456789"; | |
for($i=0;$i<$len;$i++){ | |
$ret .= $seed[rand(0,strlen($seed)-1)]; | |
} | |
return $ret; | |
} | |
public function createAction(Request $request) | |
{ | |
$json = $request->getContent(); | |
$json_array = json_decode($json,true); | |
$username = $json_array['username']; | |
$password = md5($json_array['password']); | |
$sessionId = $this->generate(20); | |
$user = new User(); | |
$user->setPassword($password); | |
$user->setUsername($username); | |
$user->setSessionId($sessionId); | |
$this->getDoctrine()->getManager()->persist($user); | |
$this->getDoctrine()->getManager()->flush(); | |
$response = new JsonResponse(); | |
$response->setData(array('sessionId'=>$sessionId)); | |
$response->setStatusCode(201); | |
return $response; | |
} | |
public function whoamiAction($sessionId) | |
{ | |
$doctrine = $this->getDoctrine(); | |
$repo = $doctrine->getRepository('MegasoftEntangleBundle:User'); | |
$user = $repo->findOneBy(array('sessionId'=>$sessionId)); | |
if($user == null){ | |
return new Response('Bad Request',400); | |
}else{ | |
$username = $user->getUsername(); | |
$response = new JsonResponse(); | |
$response->setData(array('username'=>$username)); | |
$response->setStatusCode(200); | |
return $response; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Megasoft\EntangleBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* User | |
* | |
* @ORM\Table() | |
* @ORM\Entity | |
*/ | |
class User | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="username", type="string", length=255) | |
*/ | |
private $username; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="password", type="string", length=255) | |
*/ | |
private $password; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="sessionId", type="string", length=255) | |
*/ | |
private $sessionId; | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set username | |
* | |
* @param string $username | |
* @return User | |
*/ | |
public function setUsername($username) | |
{ | |
$this->username = $username; | |
return $this; | |
} | |
/** | |
* Get username | |
* | |
* @return string | |
*/ | |
public function getUsername() | |
{ | |
return $this->username; | |
} | |
/** | |
* Set password | |
* | |
* @param string $password | |
* @return User | |
*/ | |
public function setPassword($password) | |
{ | |
$this->password = $password; | |
return $this; | |
} | |
/** | |
* Get password | |
* | |
* @return string | |
*/ | |
public function getPassword() | |
{ | |
return $this->password; | |
} | |
/** | |
* Set sessionId | |
* | |
* @param string $sessionId | |
* @return User | |
*/ | |
public function setSessionId($sessionId) | |
{ | |
$this->sessionId = $sessionId; | |
return $this; | |
} | |
/** | |
* Get sessionId | |
* | |
* @return string | |
*/ | |
public function getSessionId() | |
{ | |
return $this->sessionId; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment