<p>What you're attempting to do is very doable.  This should be of help to you:</p>

<p><a href="https://craftcms.com/classreference/services/UsersService" rel="nofollow">https://craftcms.com/classreference/services/UsersService</a></p>

<p>You can use the Yii <code>forward</code> function to pass along a request to another action handler:</p>

<pre><code>    $this-&gt;forward('users/sendNewEmailVerifyEmail', false);
</code></pre>

CC BY-SA 3.0
2015-12-04T07:48:46.257Z
I thought for sure this would be a lost cause. $this->forward() accomplished EXACTLY what I needed. Thank you!!
taylor
CC BY-SA 3.0
2015-12-04T05:56:40.053Z
2015-12-04T05:56:40.053Z
andrew.welch
<p>For those that come looking for the answer about logging in by service without the rest of the context of the question.</p>

<p>See UserSession service docs:
<a href="https://craftcms.com/classreference/services/UserSessionService#login-detail" rel="nofollow noreferrer">https://craftcms.com/classreference/services/UserSessionService#login-detail</a></p>

<p>Login with user/password:</p>

<pre><code>craft()-&gt;userSession-&gt;login($username, $password, $rememberMe = false);
</code></pre>

<p>Or when you have already authenticated and simply wish to login:</p>

<pre><code>craft()-&gt;userSession-&gt;loginByUserId($userId, $rememberMe = false, $setUsernameCookie = false);
</code></pre>

CC BY-SA 3.0
2017-11-08T04:43:48.297Z
2017-11-08T04:43:48.297Z
misteraidan
<p>I am migrating a 15,000+ users from a site's old CMS (WP), and have them set to <code>Pending.</code> All of their user information will be created and on Craft CMS, except their passwords will not be set, as migrating that is too complicated (for what I can tell.) So, the process I have in mind for previous users to login is:</p>

<ol>
<li>User enters <em>Username/Email &amp; PW</em>, submits login form.</li>
<li><p>Form sends data to custom <code>Custom Plugin Controller</code>, and follows logic:</p>

<ol>
<li><p>If <code>user status==active</code>, simply send form data to Craft's <code>users/login</code> and proceed as usual.</p></li>
<li><p>If <code>user status==pending</code>:</p>

<ol>
<li><p>Check if username/email exists in previous users list. </p>

<ol>
<li>If it does not, simply send form data to Craft's <code>users/login</code> and proceed as usual.</li>
<li>If it does exist, it sends the Activation Email and redirects to special URL stating "You've been sent an email to re-activate your account on this new website."</li>
</ol></li>
</ol></li>
</ol></li>
</ol>

<p>Is this possible to do? It would be fairly straight forward to write, except I am unsure if there is a way to communicate with Craft's <code>users/login</code> via a plugin. Is it something like <code>craft()-&gt;users-&gt;login( $userNameOrEmail, $userPassword )</code>?</p>

<p>I'm aware I could also just send out batch email activation links to the migrated users, but I'd much prefer this method, in case they miss those e-mails and try logging in at a future date. Alternatively I could have a "Are you a user of the old site? Click here to re-activate your account." Link on the login page, but again users may not notice it and think they cant login anymore.</p>

<p>Any help would be hugely appreciated! Thanks.</p>

<plugin-development><users><form><frontend><login>
Call to Craft Users Login Function from Plugin
# Call to Craft Users Login Function from Plugin

(Asked by: username_0 on 2015-12-04)

I am migrating a 15,000+ users from a site's old CMS (WP), and have them set to `Pending.` All of their user information will be created and on Craft CMS, except their passwords will not be set, as migrating that is too complicated (for what I can tell.) So, the process I have in mind for previous users to login is:

1. User enters *Username/Email & PW*, submits login form.
2. Form sends data to custom `Custom Plugin Controller`, and follows logic:

   1. If `user status==active`, simply send form data to Craft's `users/login` and proceed as usual.
   2. If `user status==pending`:

      1. Check if username/email exists in previous users list.

         1. If it does not, simply send form data to Craft's `users/login` and proceed as usual.
         2. If it does exist, it sends the Activation Email and redirects to special URL stating "You've been sent an email to re-activate your account on this new website."

Is this possible to do? It would be fairly straight forward to write, except I am unsure if there is a way to communicate with Craft's `users/login` via a plugin. Is it something like `craft()->users->login( $userNameOrEmail, $userPassword )`?

I'm aware I could also just send out batch email activation links to the migrated users, but I'd much prefer this method, in case they miss those e-mails and try logging in at a future date. Alternatively I could have a "Are you a user of the old site? Click here to re-activate your account." Link on the login page, but again users may not notice it and think they cant login anymore.

Any help would be hugely appreciated! Thanks.

**Tags:** plugin-development, users, form, frontend, login


---

## 2 answers

### Answer by username_1 on 2015-12-04. Score: 3 | Accepted answer

What you're attempting to do is very doable.  This should be of help to you:
https://craftcms.com/classreference/services/UsersService
You can use the Yii forward function to pass along a request to another action handler:
    $this->forward('users/sendNewEmailVerifyEmail', false);

> **Comments:**
> **username_0**: I thought for sure this would be a lost cause. $this->forward() accomplished EXACTLY what I needed. Thank you!!  

---

### Answer by username_2 on 2017-11-08. Score: 1

For those that come looking for the answer about logging in by service without the rest of the context of the question.
See UserSession service docs:
https://craftcms.com/classreference/services/UserSessionService#login-detail
Login with user/password:
craft()->userSession->login($username, $password, $rememberMe = false);

Or when you have already authenticated and simply wish to login:
craft()->userSession->loginByUserId($userId, $rememberMe = false, $setUsernameCookie = false);


---

