Skip to content

Commit

Permalink
Add Facebook connect and front login
Browse files Browse the repository at this point in the history
  • Loading branch information
gbeauvoir committed Jan 21, 2014
1 parent fb646ba commit c6b6900
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ parameters:
admin:
wysiwyg: true
maintenance: false

facebook:
app_id:
app_secret:

google:
oauth2_client_id:
oauth2_client_secret:
Expand Down
22 changes: 20 additions & 2 deletions app/config/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ security:
security: false

admin_area:
pattern: ^/
switch_user: true
pattern: ^/[a-z]+/administration
anonymous: ~
form_login:
login_path: _admin_login
Expand All @@ -28,10 +29,27 @@ security:
remember_me: false
logout:
path: _admin_logout
target: /
target: _admin_login
context: teel

myaccount:
pattern: ^/
anonymous: ~
form_login:
login_path: _majesteel_login
check_path: _majesteel_login_check
post_only: true
remember_me: false
logout:
path: _majesteel_logout
target: _majesteel_login
context: teel

access_control:
- { path: /[a-z]+/administration/authentification, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /[a-z]+/administration/authentification_check, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /[a-z]+/administration/, roles: ROLE_ADMIN }
- { path: /[a-z]+/my-account/authentification, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /[a-z]+/my-account/authentification_check, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /[a-z]+/my-account/, roles: ROLE_USER }
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
2 changes: 2 additions & 0 deletions app/var/db/db-mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ INSERT INTO `role` (`id`, `name`, `role`, `bundle`, `internal`, `tags`) VALUES
(1, 'Admin - is mandatory in order to have acces to admin panel', 'ROLE_ADMIN', '', 0, 'Role'),
(2, 'Admin user - has permissions to manage users', 'ROLE_ADMIN_USER', '', 0, 'Role'),
(3, 'Super admin - has all permissions', 'ROLE_SUPERADMIN', '', 0, 'Role'),
(4, 'User - access to their account', 'ROLE_USER', '', 0, 'Role'),
(9, 'Allow admin user to see all media', 'ROLE_MEDIA_LIST', 'media', 0, 'Role'),
(10, 'Allow admin user to edit or add a media', 'ROLE_MEDIA_EDIT', 'media', 0, 'Role'),
(11, 'Allow admin user to delete a media', 'ROLE_MEDIA_REMOVE', 'media', 0, 'Role'),
Expand All @@ -550,6 +551,7 @@ DROP TABLE IF EXISTS `user`;
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`media_id` int(11) DEFAULT NULL,
`facebook_id` int(11) DEFAULT NULL,
`username` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`salt` varchar(255) NOT NULL,
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"jms/security-extra-bundle": "dev-master",
"stof/doctrine-extensions-bundle": "~1.1@dev",
"symfony-cmf/routing-bundle": "1.1.*",
"google/apiclient": "dev-master",
"google/apiclient": "dev-master",
"facebook/php-sdk" : "*",
"doctrine/doctrine-migrations-bundle": "dev-master",
"friendsofsymfony/elastica-bundle": "3.0.*@dev",
"knplabs/knp-paginator-bundle": "dev-master",
Expand Down
92 changes: 92 additions & 0 deletions src/Majes/TeelBundle/Controller/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Majes\TeelBundle\Controller;

use Majes\CoreBundle\Controller\SystemController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;

class AuthController extends Controller implements SystemController
{


public function loginAction()
{
/*
* The action's view can be rendered using render() method
* or @Template annotation as demonstrated in DemoController.
*
*/

$request = $this->getRequest();
$session = $request->getSession();

$facebook_params = $session->get('facebook');
if(!empty($facebook_params['app_id'])
&& !empty($facebook_params['app_secret'])){
$facebook = new \Facebook(array(
'appId' => $facebook_params['app_id'],
'secret' => $facebook_params['app_secret'],
));

$url = 'http://'.$this->getRequest()->getHost();
$url .= $this->get('router')->generate('_majesteel_login_facebook');

$params = array(
'scope' => 'read_stream, friends_likes, email, publish_stream',
'redirect_uri' => $url
);

$facebook_url = $facebook->getLoginUrl($params);
}else
{
$facebook_url = false;
}

// get the login error if there is one
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);

return $this->render('MajesTeelBundle:Auth:login.html.twig', array('auth' => true, 'facebook_url' => $facebook_url));
}


public function loginfacebookAction(){

$request = $this->getRequest();
$session = $request->getSession();

$facebook_params = $session->get('facebook');
if(!empty($facebook_params['app_id'])
&& !empty($facebook_params['app_secret'])){
$facebook = new \Facebook(array(
'appId' => $facebook_params['app_id'],
'secret' => $facebook_params['app_secret'],
));

$user_id = $facebook->getUser();
if($user_id){

$user_profile = $facebook->api('/me','GET');

}else{
return $this->redirect($this->get('router')->generate('_majesteel_login'));
}
}else
{
$facebook_url = false;
}
//echo $this->get('router')->generate('_majesteel_account'); exit;
return $this->redirect($this->get('router')->generate('_majesteel_account'));
}

public function loginCheckAction()
{
/*
* The action's view can be rendered using render() method
* or @Template annotation as demonstrated in DemoController.
*
*/
return $this->render('MajesCoreBundle:Auth:login.html.twig');
}
}
28 changes: 28 additions & 0 deletions src/Majes/TeelBundle/Controller/MyaccountController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Majes\TeelBundle\Controller;

use Majes\CoreBundle\Controller\SystemController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use JMS\SecurityExtraBundle\Annotation\Secure;
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\HttpFoundation\Response;

class MyaccountController extends Controller implements SystemController
{

/**
* @Secure(roles="ROLE_USER")
*
*/
public function indexAction()
{
/*
* The action's view can be rendered using render() method
* or @Template annotation as demonstrated in DemoController.
*
*/
return $this->render('MajesTeelBundle:Myaccount:index.html.twig');
}

}
22 changes: 21 additions & 1 deletion src/Majes/TeelBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
_majesteel_index:
pattern: /
defaults: { _controller: MajesTeelBundle:Index:index }
defaults: { _controller: MajesTeelBundle:Index:index }

_majesteel_account:
pattern: /{_locale}/my-account
defaults: { _controller: MajesTeelBundle:Myaccount:index }

_majesteel_login:
pattern: /{_locale}/my-account/authentification
defaults: { _controller: MajesTeelBundle:Auth:login }

_majesteel_login_facebook:
pattern: /{_locale}/my-account/authentification/facebook
defaults: { _controller: MajesTeelBundle:Auth:loginfacebook }

_majesteel_login_check:
pattern: /{_locale}/my-account/authentification_check
defaults: { _controller: MajesTeelBundle:Auth:login_check }

_majesteel_logout:
pattern: /{_locale}/my-account/deconnexion
defaults: { _controller: MajesTeelBundle:Auth:logout }
52 changes: 52 additions & 0 deletions src/Majes/TeelBundle/Resources/views/Auth/login.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{% extends 'MajesTeelBundle::layout.html.twig' %}
{% block title %}{{parent()}} New symfony2 framework{% endblock %}

{% block meta_type %}{{parent()}}{% endblock %}
{% block meta_title %}{{parent()}}{% endblock %}
{% block meta_description %}{% endblock %}
{% block meta_image %}{% endblock %}


{% block content %}

{{parent()}}
<div class="container">

<div class="starter-template">
<div class="well col-md-10 col-md-offset-1">
<div class="row">
<div class="col-md-8">
<form action="{{ path('_majesteel_login_check') }}" method="post" name="login_form">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-envelope"></i></span>
<input class="form-control" type="text" name="_username" value="" />
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-lock"></i></span>
<input class="form-control" type="password" value="" name="_password"/>
</div>
</div>
<a class="pull-right" href="#">Forgot password?</a>
<div class="text-left">
<label class="checkbox">
<input type="checkbox" name="_remember_me"/>
<span>Keep me logged in</span>
</label>
</div>
<input type="submit" class="btn btn-lg btn-primary btn-block login-submit" value="Log in"/>


</form>
</div>
<div class="col-md-4">
{% if facebook_url %}<a href="{{ facebook_url }}" class="btn btn-primary btn-lg btn-block">Facebook Connect</a>{% endif %}
</div>
</div>
</div>
</div>
</div>
{% endblock %}

20 changes: 20 additions & 0 deletions src/Majes/TeelBundle/Resources/views/Myaccount/index.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'MajesTeelBundle::layout.html.twig' %}
{% block title %}{{parent()}} New symfony2 framework{% endblock %}

{% block meta_type %}{{parent()}}{% endblock %}
{% block meta_title %}{{parent()}}{% endblock %}
{% block meta_description %}{% endblock %}
{% block meta_image %}{% endblock %}


{% block content %}

{{parent()}}
<div class="container">

<div class="starter-template">
<h1>My account</h1>
</div>
</div>
{% endblock %}

19 changes: 18 additions & 1 deletion src/Majes/TeelBundle/Resources/views/parts/menu.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,25 @@
{% else %}<a href="{% if menu_page.link_url is null %}{{ path('majes_cms_' ~ menu_page.id ~ '_' ~ lang) }}{% else %}{{menu_page.link_url}}{% endif %}"{% if menu_page.target_url is not null %}target="{{ menu_page.target_url }}"{% endif %}>{% endif %}{{ menu_page.label }}</a>
</li>
{% endfor %}

</ul>
<ul class="nav navbar-nav navbar-right">
{% if app.user is not null %}<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">{{app.user.email}} <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="{{ path('_majesteel_logout') }}">Disconnect</a></li>
</ul>
</li>{% else %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">My account <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="{{ path('_majesteel_login') }}">Login</a></li>
<li><a href="{{ path('_majesteel_login') }}">Subscribe</a></li>
</ul>
</li>
{% endif %}
</ul>

</div><!--/.nav-collapse -->
</div>
</div>

0 comments on commit c6b6900

Please sign in to comment.