eZ Community » Forums » eZ Platform » Programmatic Login
expandshrink

Programmatic Login

Programmatic Login

Tuesday 25 July 2017 3:57:34 pm - 1 reply

Hello everyone,

I have a ez6 application and a registration form. After the user did the registration, I am trying to login the user automatically. 

I have the following code at the moment but I get redirected to the login page with infinite redirects

<span>$contentTypeIdentifier </span><span>= </span><span>$this</span><span>-></span><span>getConfigResolver</span><span>()-></span><span>getParameter</span><span>(</span><span>'user.content_type_identifier'</span><span>, </span><span>'ngmore'</span><span>)</span><span>;
</span><span>$contentType </span><span>= </span><span>$this</span><span>-></span><span>getRepository</span><span>()-></span><span>getContentTypeService</span><span>()-></span><span>loadContentTypeByIdentifier</span><span>(</span><span>$contentTypeIdentifier</span><span>)</span><span>;
</span><span>$languages </span><span>= </span><span>$this</span><span>-></span><span>getConfigResolver</span><span>()-></span><span>getParameter</span><span>(</span><span>'languages'</span><span>)</span><span>;
</span><span>
</span><span>
</span><span>/** </span><span>@var </span><span>UserCreateStruct $userCreateStruct */
</span><span>$userCreateStruct </span><span>= </span><span>$this</span><span>-></span><span>get</span><span>(</span><span>'ezpublish.api.service.user'</span><span>)-></span><span>newUserCreateStruct</span><span>(
</span><span>    </span><span>null</span><span>,
</span><span>    </span><span>null</span><span>,
</span><span>    </span><span>null</span><span>,
</span><span>    </span><span>$languages</span><span>[</span><span>0</span><span>]</span><span>,
</span><span>    </span><span>$contentType
</span><span>)</span><span>;
</span><span>
</span><span>$userCreateStruct</span><span>-></span><span>enabled </span><span>= </span><span>true</span><span>;
</span><span>$userCreateStruct</span><span>-></span><span>alwaysAvailable </span><span>= </span><span>true</span><span>;
</span><span>
</span><span>$userCreateStruct</span><span>-></span><span>setField</span><span>(</span><span>'salut'</span><span>, </span><span>new </span><span>Value([</span><span>0</span><span>]))</span><span>;
</span><span>$userCreateStruct</span><span>-></span><span>setField</span><span>(</span><span>'first_name'</span><span>, </span><span>$customer</span><span>-></span><span>getFirstName</span><span>())</span><span>;
</span><span>$userCreateStruct</span><span>-></span><span>setField</span><span>(</span><span>'last_name'</span><span>, </span><span>$customer</span><span>-></span><span>getLastName</span><span>())</span><span>;
</span><span>
</span><span>$userCreateStruct</span><span>-></span><span>setField</span><span>(</span><span>'address'</span><span>, </span><span>$billingAddress</span><span>-></span><span>getStreet</span><span>())</span><span>;
</span><span>$userCreateStruct</span><span>-></span><span>setField</span><span>(</span><span>'zip'</span><span>, </span><span>$billingAddress</span><span>-></span><span>getPostcode</span><span>())</span><span>;
</span><span>$userCreateStruct</span><span>-></span><span>setField</span><span>(</span><span>'city'</span><span>, </span><span>$billingAddress</span><span>-></span><span>getCity</span><span>())</span><span>;
</span><span>$userCreateStruct</span><span>-></span><span>setField</span><span>(</span><span>'new_attribute7'</span><span>, </span><span>new </span><span>Value([</span><span>0</span><span>]))</span><span>;
</span><span>$userCreateStruct</span><span>-></span><span>setField</span><span>(</span><span>'phone'</span><span>, </span><span>$customer</span><span>-></span><span>getPhone</span><span>())</span><span>;
</span><span>$userCreateStruct</span><span>-></span><span>setField</span><span>(</span><span>'fax'</span><span>, </span><span>$customer</span><span>-></span><span>getFax</span><span>())</span><span>;
</span><span>
</span><span>$userCreateStruct</span><span>-></span><span>login </span><span>= </span><span>$customer</span><span>-></span><span>getEmail</span><span>()</span><span>;
</span><span>$userCreateStruct</span><span>-></span><span>email </span><span>= </span><span>$customer</span><span>-></span><span>getEmail</span><span>()</span><span>;
</span><span>$userCreateStruct</span><span>-></span><span>password </span><span>= </span><span>$userData</span><span>[</span><span>'password'</span><span>]</span><span>;
</span><span>
</span><span>$users </span><span>= </span><span>$this</span><span>-></span><span>get</span><span>(</span><span>'ezpublish.api.service.user'</span><span>)-></span><span>loadUsersByEmail</span><span>(</span><span>$userCreateStruct</span><span>-></span><span>email</span><span>)</span><span>;
</span><span>
</span><span>
</span><span>if </span><span>(!</span><span>empty</span><span>(</span><span>$users</span><span>)) {
</span><span>    </span><span>throw new </span><span>UserConflictException()</span><span>;
</span><span>}
</span><span>
</span><span>$userGroupId </span><span>= </span><span>$this</span><span>-></span><span>getConfigResolver</span><span>()-></span><span>getParameter</span><span>(</span><span>'user.user_group_content_id'</span><span>, </span><span>'ngmore'</span><span>)</span><span>;
</span><span>
</span><span>$preUserRegisterEvent </span><span>= </span><span>new </span><span>UserEvents\PreRegisterEvent(</span><span>$userCreateStruct</span><span>)</span><span>;
</span><span>$this</span><span>-></span><span>get</span><span>(</span><span>'event_dispatcher'</span><span>)-></span><span>dispatch</span><span>(MVCEvents::</span><span>USER_PRE_REGISTER</span><span>, </span><span>$preUserRegisterEvent</span><span>)</span><span>;
</span><span>$userCreateStruct </span><span>= </span><span>$preUserRegisterEvent</span><span>-></span><span>getUserCreateStruct</span><span>()</span><span>;
</span><span>
</span><span>/** </span><span>@var </span><span>\eZ\Publish\API\Repository\Values\User\User $newUser */
</span><span>$newUser </span><span>= </span><span>$this</span><span>-></span><span>getRepository</span><span>()-></span><span>sudo</span><span>(
</span><span>    </span><span>function </span><span>(Repository </span><span>$repository</span><span>) </span><span>use </span><span>(</span><span>$userCreateStruct</span><span>, </span><span>$userGroupId</span><span>) {
</span><span>        </span><span>$userGroup </span><span>= </span><span>$repository</span><span>-></span><span>getUserService</span><span>()-></span><span>loadUserGroup</span><span>(</span><span>$userGroupId</span><span>)</span><span>;
</span><span>
</span><span>        </span><span>return </span><span>$repository</span><span>-></span><span>getUserService</span><span>()-></span><span>createUser</span><span>(
</span><span>            </span><span>$userCreateStruct</span><span>,
</span><span>            </span><span>array</span><span>(</span><span>$userGroup</span><span>)
</span><span>        )</span><span>;
</span><span>    </span><span>}
</span><span>)</span><span>;
</span><span>
</span><span>
</span><span>$userRegisterEvent </span><span>= </span><span>new </span><span>UserEvents\PostRegisterEvent(</span><span>$newUser</span><span>)</span><span>;
</span><span>$this</span><span>-></span><span>get</span><span>(</span><span>'event_dispatcher'</span><span>)-></span><span>dispatch</span><span>(MVCEvents::</span><span>USER_POST_REGISTER</span><span>, </span><span>$userRegisterEvent</span><span>)</span><span>;
</span><span>
</span><span>
</span><span>$symfonyUser </span><span>= </span><span>$this
</span><span>    </span><span>-></span><span>get</span><span>(</span><span>'ezpublish.security.user_provider'</span><span>)
</span><span>    -></span><span>loadUserByAPIUser</span><span>(</span><span>$newUser</span><span>)</span><span>;</span>
<span>$token </span><span>= </span><span>new </span><span>UsernamePasswordToken(
</span><span>    </span><span>$symfonyUser</span><span>,
</span><span>    </span><span>$password</span><span>,
</span><span>    </span><span>"ezpublish.security.user_provider"</span><span>,
</span><span>    </span><span>$symfonyUser</span><span>-></span><span>getRoles</span><span>()
</span><span>)</span><span>;
</span><span>
</span><span>$this</span><span>-></span><span>get</span><span>(</span><span>"security.token_storage"</span><span>)-></span><span>setToken</span><span>(</span><span>$token</span><span>)</span><span>;
</span><span>$event </span><span>= </span><span>new </span><span>InteractiveLoginEvent(</span><span>$request</span><span>, </span><span>$token</span><span>)</span><span>;
</span><span>$this</span><span>-></span><span>get</span><span>(</span><span>"event_dispatcher"</span><span>)-></span><span>dispatch</span><span>(</span><span>"security.interactive_login"</span><span>, </span><span>$event</span><span>)</span><span>;</span>

Tuesday 22 August 2017 1:05:19 am

Hello,

Could you update your source code example snippet and remove the <span> tags?

It's very hard to help when we can not clearly see the code your trying to share.

Cheers,
Heath

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from