Symfony 3 Login as Startpage - fosuserbundle

Simple ToDO:
i want that when i go www.mysmyfonyproject.com,
that , when user is not logged in, than the login-page
should shown.
Otherwise the user-landingpage should shown.
i try to make a bad solution in my home controller like this:
/**
* #Route("/")
*/
public function indexAction()
{
if (
! $this->get('security.authorization_checker')
->isGranted('IS_AUTHENTICATED_FULLY')
)
{
return $this->redirectToRoute('login');
} else {
return $this->redirectToRoute('user-landingpage');
}
}
but here is a ERROR : unknown path /login.

Related

Cakephp how to redirect from beforeRender method in AppController?

User case I'm trying to do, if a user not submit his company details always redirect user to company page.
In appController I have written like below beforeRender method
public function beforeRender(\Cake\Event\EventInterface $event) {
if( !is_null($this->Auth) )
{
if($this->getTable('Users')->hasCompany($this->Auth) == false){
return $this->redirect(['controller'=>'Companies','action'=>'edit']);
}
}
}
Here hasCompany method always return true or false. Here I'm trying to redirect user if hasCompany == false. But problem is like infinite loop browser just reloading.Redirection are not working.

Passing result array to controller in codeigniter

I am developing a simple profile page but I am having a little dificulty passing the result array to the controller..am not sure if this how to do it.
Model:
class Login_model extends CI_Model{
function get_profile()
{
$this->db->where('email', $this->input->post('email'));
$query = $this->db->get('user');
if($query->num_rows == 1)
{
return $query->result_array();
}
}
}
Controller:
class Login extends CI_Controller {
function show_profile{
$this->load->model('login_model');
$q['user'] = $this->login_model->get_profile();
echo $q['user'];
}
}
I tried to echo out $q['user'] as a tests to see if it will work, but it did not work.
in this context a result array would be if you were getting back more then one result. since you are only getting back one result you can do row_array() so like
if($query->num_rows == 1)
{
return $query->row_array();
}
else
{
return false;
}
NEVER do this
// example of what NOT to do
$this->db->where('email', $this->input->post('email'));// NO NO NO
validate the email first using codeigniter form validation and then send it like
// if no user comes back then deal with it right away
if( ! $user = $this->login_model->get_profile($email) )
{
$this->showNoUserFor($email) ;
}
else
{ // carry on....
otherwise - do the tutorial first in the codeigniter manual, it will save you a lot of time.
you cannot echo $q['user'] because it is an array. Use
`print_r($q['user']); exit;
and you can pass this to your view page by
`$this->load->view('yourViewPage',$q);

How can I show 404 error page when the ID is null?

I create a table in MYSQL. I insert some data into the table.
My question is about error when ID is NULL.
When the URL is ".com/blog/post/1" The content has ID 1 seen perfectly as well as the other ID's contents. But the problem is that I couldn't solve (because I am newbie in Codeigniter) when the url is ".com/blog/post" an error occurs.
I think the problem the "post/" needs and ID number after itself but how to change the error with a page like this shown below
Forbidden
You don't have permission to access /post/ on this server.
In your blog controller when somebody calls for a post then you can check if id is there, if not you can redirect them to blog index.
The blog controller will look like this.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Blog extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
//List posts with title and excerpts and a read more link
}
function post($id=NULL)
{
if($id)
{
//Show detailed post
}
else
{
redirect('blog');
}
}
}
You can pass the ID to the function as a parameter; Like this;
public function post($id = false)
{
if (!$id)
{
// No ID supplied
}
else
{
// ID found.
// Carry on....
}
}
Hope this helps.

Bug into fosuserbundle when double click on confirmation link?

I just begin to use fosuserbundle, today I activate the confirmation register link.
It works great, but if the user click a second time on the confirmation link in the email, he get that error :
The user with confirmation token "3hiqollkisg0s4ck4w8g0gw4soc0wwoo8ko084o4ww4sss8o4" does not exist
404 Not Found - NotFoundHttpException
I think this error should be handle by the bundle, no ?
Thanks
Here's the code for overriding the action. Basically just copied part of the actual FOS action and modded.
Create a RegistrationController.php file in your user bundle's controller folder and put the overriding RegistrationController class in there.
Assuming your user bundle is Acme\UserBundle:
<?php
// Acme\UserBundle\RegistrationController.php
namespace Acme\UserBundle\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class RegistrationController extends BaseController
{
/**
* Receive the confirmation token from user email provider, login the user
*/
public function confirmAction(Request $request, $token)
{
$userManager = $this->container->get('fos_user.user_manager');
$user = $userManager->findUserByConfirmationToken($token);
if (null === $user) {
/* ************************************
*
* User with token not found. Do whatever you want here
*
* e.g. redirect to login:
*
* return new RedirectResponse($this->container->get('router')->generate('fos_user_security_login'));
*
**************************************/
}
else{
// Token found. Letting the FOSUserBundle's action handle the confirmation
return parent::confirmAction($request, $token);
}
}
}

Sample for Edit Url for a FB Page Tab App

I am implementing the edit url FB Page Tab App. But it needs the initialization which should happen when the app is added to a fanpage by the admin.
I am looking for the initial callback/notification to my app-url when the app is loaded on the fanpage. (I have looked at this already - http://developers.facebook.com/docs/authentication/signed_request/).
I am looking for a sample that shows the handling of the signed_request in this case, from the fan-page-load and what details are available/etc..
Thanks !
Here's a sample of handling the signed request:
function parse_signed_request($signed_request, $secret) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}
// check sig
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
in $data there will be a "page" object that has a "admin" boolean. This will tell you if the current user of the page tab application is an admin of the page that the app is a tab of.

Resources