Esempio n. 1
0
 public function createMember($rest)
 {
     $post = $rest->getRequest()->getPost();
     MM_LogApi::logRequest(json_encode($post), "/createMember");
     if (!Utils::isAuthenticated($post)) {
         return new Response($rest, null, RESPONSE_ERROR_MESSAGE_AUTH, RESPONSE_ERROR_CODE_AUTH, RESPONSE_ERROR_MESSAGE_AUTH);
     }
     $req = new stdClass();
     $req->membership_level_id = self::$REGEX_INTEGER_ONLY;
     $req->email = self::$REGEX_CONTAINS_EMAIL;
     $data = Utils::processApiRequestData($post, $req);
     if (MM_Response::isError($data)) {
         return new Response($rest, null, $data->message, RESPONSE_ERROR_CODE_MISSING_PARAMS, RESPONSE_ERROR_MESSAGE_MISSING_PARAMS);
     }
     $result = MM_APIService::createMember($data);
     if (MM_Response::isSuccess($result)) {
         $user = MM_User::findByEmail($data->email);
         if (!$user->isValid()) {
             return new Response($rest, null, "Failed to create user with email address {$data->email}", RESPONSE_ERROR_CODE_CONFLICT, RESPONSE_ERROR_MESSAGE_CONFLICT);
         }
         $userData = array('member_id' => $user->getId(), 'username' => $user->getUsername(), 'email' => $user->getEmail(), 'password' => $user->getPassword(), 'confirmationUrl' => $result->getData(MM_Response::$DATA_KEY_URL));
         return new Response($rest, $userData, $userData);
     } else {
         return new Response($rest, null, $result->message, RESPONSE_ERROR_CODE_CONFLICT, RESPONSE_ERROR_MESSAGE_CONFLICT);
     }
 }
Esempio n. 2
0
function processLogin($request, $provider)
{
    //don't attempt to login already logged in users
    if (MM_Utils::isLoggedIn()) {
        $loggedInUser = MM_Utils::getCurrentUser();
        $redirectUrl = isset($request['redirect_url']) ? $request['redirect_url'] : MM_CorePageEngine::getUrl(MM_CorePageType::$MEMBER_HOME_PAGE, null, $loggedInUser);
        wp_redirect($redirectUrl);
        exit;
    }
    //either login using a linked account, or using the email (if provided) by the social media account, with that order of precedence
    $authResponse = $provider->authenticate();
    if (!MM_Response::isSuccess($authResponse)) {
        //error authenticating
        throw new Exception("Error authenticating with social network", "1001015");
    }
    $profileResponse = $provider->getUserProfile();
    if (!MM_Response::isSuccess($profileResponse)) {
        //error retrieving profile
        throw new Exception("Error retrieving social network profile", "1001005");
    }
    $profile = $profileResponse->message;
    if (!isset($profile->identifier) || empty($profile->identifier)) {
        //invalid profile identifier
        throw new Exception("Error retrieving social network profile identier or identifier was invalid", "1001006");
    }
    $socialNetworkUniqueIdentifier = $profile->identifier;
    $userAccountResponse = $provider->findLinkedUserByIdentifier($socialNetworkUniqueIdentifier);
    if (MM_Response::isSuccess($userAccountResponse)) {
        $loginUser = $userAccountResponse->message;
    } else {
        if ($provider->getEmailHandlingStrategy() == MM_AbstractSocialLoginExtension::$EMAIL_PROVIDED) {
            //couldnt locate a linked account, either because it doesnt exist or there was an error, try using profile email
            $email = isset($profile->emailVerified) ? $profile->emailVerified : (isset($profile->email) ? $profile->email : "");
            if (empty($email)) {
                throw new Exception("Unable to login: account not linked and no user account found with the supplied email", "1001002");
            }
            $loginUser = MM_User::findByEmail($email);
            if (!$loginUser->isValid()) {
                throw new Exception("Unable to login: account not linked and no valid user account found with the supplied email", "1001003");
            }
        } else {
            throw new Exception("Unable to login: account not linked and provider doesn't supply email", "1001001");
        }
    }
    //we have the user now
    $userHooks = new MM_UserHooks();
    $redirectUrl = isset($request['redirect_url']) ? $request['redirect_url'] : MM_CorePageEngine::getUrl(MM_CorePageType::$MEMBER_HOME_PAGE, null, $loginUser);
    $userHooks->doAutoLogin($loginUser->getId(), $redirectUrl);
    //end login block
    exit;
}