| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Services\Helper; |
| 6 |
use FluentSupport\Framework\Support\Arr; |
| 7 |
use FluentSupport\Framework\Request\Request; |
| 8 |
use FluentSupport\App\Hooks\Handlers\AuthHandler; |
| 9 |
|
| 10 |
class AuthController extends Controller |
| 11 |
{ |
| 12 |
/** |
| 13 |
* signUp method will create new user submitted data from sign up form |
| 14 |
* @param Request $request |
| 15 |
* @return \WP_REST_Response |
| 16 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 17 |
*/ |
| 18 |
public function signup(Request $request) |
| 19 |
{ |
| 20 |
|
| 21 |
if (!wp_verify_nonce($request->get('_fsupport_signup_nonce'), 'fluent_support_signup_nonce')) { |
| 22 |
return $this->sendError([ |
| 23 |
'message' => __('Security verification failed. Please try again', 'fluent-support') |
| 24 |
]); |
| 25 |
} |
| 26 |
|
| 27 |
$fields = AuthHandler::getSignupFields(); |
| 28 |
|
| 29 |
$rules = $this->getRules($fields); |
| 30 |
|
| 31 |
$messages = $this->getMessages($rules); |
| 32 |
|
| 33 |
/* |
| 34 |
* Filter user signup form data |
| 35 |
* |
| 36 |
* @since v1.0.0 |
| 37 |
* @param array $formData |
| 38 |
*/ |
| 39 |
$formData = apply_filters('fluent_support/signup_form_data', $request->all()); |
| 40 |
|
| 41 |
/* |
| 42 |
* Action before validate user signup |
| 43 |
* |
| 44 |
* @since v1.0.0 |
| 45 |
* @param array $formData |
| 46 |
*/ |
| 47 |
do_action('fluent_support/before_signup_validation', $formData); |
| 48 |
|
| 49 |
$this->validate($formData, $rules, $messages); |
| 50 |
|
| 51 |
/* |
| 52 |
* Action After validate user signup validation success |
| 53 |
* |
| 54 |
* @since v1.0.0 |
| 55 |
* @param array $formData |
| 56 |
*/ |
| 57 |
do_action('fluent_support/after_signup_validation', $formData); |
| 58 |
|
| 59 |
$userId = $this->createUser($formData); |
| 60 |
|
| 61 |
if (is_wp_error($userId)) { |
| 62 |
return $this->response( |
| 63 |
apply_filters( |
| 64 |
'fluent_support/signup_create_user_error', |
| 65 |
['error' => $userId->get_error_message()] |
| 66 |
), 423); |
| 67 |
} |
| 68 |
|
| 69 |
/* |
| 70 |
* Action After creating WP user from ticket sign up form |
| 71 |
* |
| 72 |
* @since v1.0.0 |
| 73 |
* @param array $formData |
| 74 |
*/ |
| 75 |
do_action('fluent_support/after_creating_user'); |
| 76 |
|
| 77 |
$this->maybeUpdateUser($userId, $formData); |
| 78 |
$this->assignRole($userId); |
| 79 |
$this->login($userId); |
| 80 |
|
| 81 |
/* |
| 82 |
* Filter for user signup complete message and redirect |
| 83 |
* |
| 84 |
* @since v1.0.0 |
| 85 |
* @param array $response |
| 86 |
*/ |
| 87 |
$response = apply_filters('fluent_support/signup_complete_response', [ |
| 88 |
'message' => __('Successfully registered to the site.', 'fluent-support'), |
| 89 |
'redirect' => Arr::get($formData, '__redirect_to', Helper::getPortalBaseUrl()) |
| 90 |
]); |
| 91 |
|
| 92 |
return $this->response($response); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* handleLogin method will perform login functionality and redirect |
| 97 |
* @param Request $request |
| 98 |
* @return \WP_REST_Response |
| 99 |
*/ |
| 100 |
public function handleLogin(Request $request) |
| 101 |
{ |
| 102 |
if (!wp_verify_nonce($request->get('_support_login_nonce'), 'fsupport_login_nonce')) { |
| 103 |
return $this->response([ |
| 104 |
'message' => __('Security verification failed', 'fluent-support') |
| 105 |
], 403); |
| 106 |
} |
| 107 |
|
| 108 |
$data = $request->all(); |
| 109 |
|
| 110 |
if (empty($data['pwd']) || empty($data['log'])) { |
| 111 |
return $this->response([ |
| 112 |
'message' => __('Email and Password is required', 'fluent-support') |
| 113 |
], 403); |
| 114 |
} |
| 115 |
|
| 116 |
$redirectUrl = Helper::getPortalBaseUrl(); |
| 117 |
|
| 118 |
if (get_current_user_id()) { // user already registered |
| 119 |
return $this->sendSuccess([ |
| 120 |
'redirect' => $redirectUrl |
| 121 |
]); |
| 122 |
} |
| 123 |
|
| 124 |
$email = $data['log']; |
| 125 |
$password = $data['pwd']; |
| 126 |
|
| 127 |
if (is_email($email)) { |
| 128 |
$user = get_user_by('email', $email); |
| 129 |
} else { |
| 130 |
$user = get_user_by('login', $email); |
| 131 |
} |
| 132 |
|
| 133 |
if (!$user) { |
| 134 |
return $this->response([ |
| 135 |
'message' => __('Email or Password is not valid. Please try again', 'fluent-support') |
| 136 |
], 403); |
| 137 |
} |
| 138 |
|
| 139 |
$info = array( |
| 140 |
'user_login' => sanitize_text_field(trim($_POST['log'])), |
| 141 |
'user_password' => trim($_POST['pwd']), |
| 142 |
'remember' => isset($_POST['rememberme']), |
| 143 |
); |
| 144 |
|
| 145 |
if (apply_filters('fluent_support_use_native_login', true)) { |
| 146 |
return $this->nativeLoginHandler($user, $info, $redirectUrl); |
| 147 |
} |
| 148 |
|
| 149 |
if (wp_check_password($password, $user->user_pass, $user->ID)) { |
| 150 |
$this->login($user->ID); |
| 151 |
return $this->sendSuccess([ |
| 152 |
'redirect' => $redirectUrl |
| 153 |
]); |
| 154 |
} |
| 155 |
|
| 156 |
return $this->response([ |
| 157 |
'message' => __('Email or Password is not valid. Please try again', 'fluent-support') |
| 158 |
], 403); |
| 159 |
} |
| 160 |
|
| 161 |
private function nativeLoginHandler($user, $info, $redirectUrl = '') |
| 162 |
{ |
| 163 |
if (!$redirectUrl) { |
| 164 |
$redirectUrl = Helper::getPortalBaseUrl(); |
| 165 |
} |
| 166 |
|
| 167 |
$secure_cookie = is_ssl(); |
| 168 |
if (!$secure_cookie && !force_ssl_admin()) { |
| 169 |
if (get_user_option('use_ssl', $user->ID)) { |
| 170 |
$secure_cookie = true; |
| 171 |
force_ssl_admin(true); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if (class_exists('\Limit_Login_Attempts')) { |
| 176 |
global $limit_login_attempts_obj; |
| 177 |
$limit_login_attempts_try = $limit_login_attempts_obj->wp_authenticate_user($user, false); |
| 178 |
if (is_wp_error($limit_login_attempts_try)) { |
| 179 |
return $this->response([ |
| 180 |
'message' => implode('<br/>', $limit_login_attempts_try->get_error_messages()) |
| 181 |
], 403); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
$user_signon = wp_signon($info, $secure_cookie); |
| 186 |
|
| 187 |
if (!is_wp_error($user_signon) && empty($_COOKIE[LOGGED_IN_COOKIE])) { |
| 188 |
if (headers_sent()) { |
| 189 |
return $this->response([ |
| 190 |
'message' => sprintf(__('<strong>ERROR</strong>: Cookies are blocked due to unexpected output. For help, please see <a href="%1$s">this documentation</a> or try the <a href="%2$s">support forums</a>.'), |
| 191 |
__('https://codex.wordpress.org/Cookies'), __('https://wordpress.org/support/')) |
| 192 |
], 403); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
if (is_wp_error($user_signon)) { |
| 197 |
$errorMessage = __('Email or Password is not valid. Please try again', 'fluent-support'); |
| 198 |
|
| 199 |
if (class_exists('Limit_Login_Attempts')) { |
| 200 |
global $limit_login_attempts_obj; |
| 201 |
if ($limit_login_attempts_obj) { |
| 202 |
$limit_login_attempts_obj->limit_login_failed($user->user_login); |
| 203 |
$msg = $limit_login_attempts_obj->get_message(); |
| 204 |
if ($msg) { |
| 205 |
$errorMessage = $msg; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
return $this->response([ |
| 211 |
'message' => $errorMessage |
| 212 |
], 403); |
| 213 |
} |
| 214 |
|
| 215 |
// WP Last Login plugin compatibility |
| 216 |
if (class_exists('\Obenland_Wp_Last_Login')) { |
| 217 |
update_user_meta($user_signon->ID, 'wp-last-login', time()); |
| 218 |
} |
| 219 |
|
| 220 |
return $this->sendSuccess([ |
| 221 |
'redirect' => $redirectUrl |
| 222 |
]); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* getRules method will prepare the rules for the input field |
| 227 |
* @param array $fields |
| 228 |
* @return mixed |
| 229 |
*/ |
| 230 |
protected function getRules($fields = []) |
| 231 |
{ |
| 232 |
$rules = []; |
| 233 |
|
| 234 |
foreach ($fields as $fieldName => $field) { |
| 235 |
if (array_key_exists('required', $field)) { |
| 236 |
$rules[$fieldName] = 'required'; |
| 237 |
} |
| 238 |
|
| 239 |
$pipe = array_key_exists($fieldName, $rules) ? '|' : ''; |
| 240 |
|
| 241 |
if ($field['type'] === 'email') { |
| 242 |
$rules[$fieldName] = $rules[$fieldName] . $pipe . 'email'; |
| 243 |
} elseif ($field['type'] === 'password') { |
| 244 |
$rules[$fieldName] = $rules[$fieldName] . $pipe . 'min:8'; |
| 245 |
} |
| 246 |
} |
| 247 |
/* |
| 248 |
* Filter user signup validation rules |
| 249 |
* |
| 250 |
* @since v1.0.0 |
| 251 |
* @param array $rules |
| 252 |
*/ |
| 253 |
return apply_filters('fluent_support/signup_validation_rules', $rules); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
/** |
| 258 |
* getMessages message will return the validation message regarding sign up or sign in |
| 259 |
* @param array $rules |
| 260 |
* @return mixed |
| 261 |
*/ |
| 262 |
protected function getMessages($rules = []) |
| 263 |
{ |
| 264 |
/* |
| 265 |
* Filter user signup validation message |
| 266 |
* |
| 267 |
* @since v1.0.0 |
| 268 |
* @param array $arg |
| 269 |
* @param array $rules |
| 270 |
*/ |
| 271 |
return apply_filters('fluent_support/signup_validation_messages', [], $rules); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* createUser method will create new user |
| 276 |
* @param array $formData |
| 277 |
* @return mixed |
| 278 |
*/ |
| 279 |
public function createUser($formData = []) |
| 280 |
{ |
| 281 |
/* |
| 282 |
* Filter user signup email |
| 283 |
* |
| 284 |
* @since v1.0.0 |
| 285 |
* @param string $email |
| 286 |
*/ |
| 287 |
$email = apply_filters('fluent_support/signup_email', Arr::get($formData, 'email')); |
| 288 |
|
| 289 |
/* |
| 290 |
* Filter user signup username |
| 291 |
* |
| 292 |
* @since v1.0.0 |
| 293 |
* @param string $username |
| 294 |
*/ |
| 295 |
$userName = apply_filters('fluent_support/signup_username', Arr::get($formData, 'username')); |
| 296 |
|
| 297 |
if (empty($formData['password'])) { |
| 298 |
$password = wp_generate_password(8); |
| 299 |
} else { |
| 300 |
$password = $formData['password']; |
| 301 |
} |
| 302 |
|
| 303 |
/* |
| 304 |
* Filter user signup password |
| 305 |
* |
| 306 |
* @since v1.0.0 |
| 307 |
* @param string $password |
| 308 |
*/ |
| 309 |
$password = apply_filters('fluent_support/signup_password', $password); |
| 310 |
|
| 311 |
/* |
| 312 |
* Action before creating WP user using Fluent Support signup form |
| 313 |
* |
| 314 |
* @since v1.0.0 |
| 315 |
* @param string $userName |
| 316 |
* @param string $password |
| 317 |
* @param string $email |
| 318 |
*/ |
| 319 |
do_action('fluent_support/before_creating_user', $userName, $password, $email); |
| 320 |
|
| 321 |
return wp_create_user($userName, $password, $email); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* maybeUpdateUser method will update user information if exists |
| 326 |
* @param $userId |
| 327 |
* @param $formData |
| 328 |
*/ |
| 329 |
public function maybeUpdateUser($userId, $formData) |
| 330 |
{ |
| 331 |
$name = trim(Arr::get($formData, 'first_name') . ' ' . Arr::get($formData, 'last_name')); |
| 332 |
|
| 333 |
$data = array_filter([ |
| 334 |
'ID' => $userId, |
| 335 |
'user_nicename' => $name, |
| 336 |
'display_name' => $name, |
| 337 |
'first_name' => Arr::get($formData, 'first_name'), |
| 338 |
'last_name' => Arr::get($formData, 'last_name'), |
| 339 |
]); |
| 340 |
|
| 341 |
if ($name) { |
| 342 |
/* |
| 343 |
* Action before updating a customer/user |
| 344 |
* |
| 345 |
* @since v1.0.0 |
| 346 |
* @param array $data |
| 347 |
*/ |
| 348 |
do_action('fluent_support/before_updating_user', $data); |
| 349 |
|
| 350 |
/* |
| 351 |
* Filter user updatable data |
| 352 |
* |
| 353 |
* @since v1.0.0 |
| 354 |
* @param $data |
| 355 |
*/ |
| 356 |
$updateUserData = apply_filters('fluent_support/update_user_data', $data); |
| 357 |
wp_update_user($updateUserData); |
| 358 |
|
| 359 |
/* |
| 360 |
* Action after updating a customer/user |
| 361 |
* |
| 362 |
* @since v1.0.0 |
| 363 |
* @param array $data |
| 364 |
*/ |
| 365 |
do_action('fluent_support/after_updating_user', $data); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* assignRole method will assign role to a given user id |
| 371 |
* @param $userId |
| 372 |
*/ |
| 373 |
protected function assignRole($userId) |
| 374 |
{ |
| 375 |
$user = new \WP_User($userId); |
| 376 |
|
| 377 |
/* |
| 378 |
* Action before assigning role to registered user |
| 379 |
* |
| 380 |
* @since v1.0.0 |
| 381 |
* @param array $data |
| 382 |
*/ |
| 383 |
do_action('fluent_support/before_assigning_role', $user); |
| 384 |
/* |
| 385 |
* Filter user assignable role after signup |
| 386 |
* |
| 387 |
* @since v1.0.0 |
| 388 |
* @param string $setRole WordPress user role key |
| 389 |
*/ |
| 390 |
$setRole = apply_filters('fluent_support/user_role', 'subscriber'); |
| 391 |
$user->set_role($setRole); |
| 392 |
|
| 393 |
/* |
| 394 |
* Action after assigning role to registered user |
| 395 |
* |
| 396 |
* @since v1.0.0 |
| 397 |
* @param array $data |
| 398 |
*/ |
| 399 |
do_action('fluent_support/after_assigning_role', $user); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
/** |
| 404 |
* login method will clear existing cookies and set new cookie for a given user id |
| 405 |
* @param $userId |
| 406 |
*/ |
| 407 |
protected function login($userId) |
| 408 |
{ |
| 409 |
/* |
| 410 |
* Action before login |
| 411 |
* |
| 412 |
* @since v1.0.0 |
| 413 |
* @param integer $userId |
| 414 |
*/ |
| 415 |
do_action('fluent_support/before_logging_in_user', $userId); |
| 416 |
|
| 417 |
wp_clear_auth_cookie(); |
| 418 |
wp_set_current_user($userId); |
| 419 |
wp_set_auth_cookie($userId); |
| 420 |
|
| 421 |
/* |
| 422 |
* Action after login |
| 423 |
* |
| 424 |
* @since v1.0.0 |
| 425 |
* @param integer $userId |
| 426 |
*/ |
| 427 |
do_action('fluent_support/after_logging_in_user', $userId); |
| 428 |
} |
| 429 |
} |
| 430 |
|