activities_helper.php
1 year ago
agent_helper.php
1 year ago
auth_helper.php
1 year ago
blocks_helper.php
1 year ago
booking_helper.php
1 year ago
bricks_helper.php
1 year ago
bundles_helper.php
1 year ago
calendar_helper.php
1 year ago
carts_helper.php
1 year ago
connector_helper.php
1 year ago
csv_helper.php
1 year ago
customer_helper.php
1 year ago
database_helper.php
1 year ago
debug_helper.php
1 year ago
defaults_helper.php
1 year ago
elementor_helper.php
1 year ago
email_helper.php
1 year ago
encrypt_helper.php
1 year ago
events_helper.php
1 year ago
form_helper.php
1 year ago
icalendar_helper.php
1 year ago
image_helper.php
1 year ago
invoices_helper.php
1 year ago
license_helper.php
1 year ago
location_helper.php
1 year ago
marketing_systems_helper.php
1 year ago
meeting_systems_helper.php
1 year ago
menu_helper.php
1 year ago
meta_helper.php
1 year ago
migrations_helper.php
1 year ago
money_helper.php
1 year ago
notifications_helper.php
1 year ago
order_intent_helper.php
1 year ago
orders_helper.php
1 year ago
pages_helper.php
1 year ago
params_helper.php
1 year ago
payments_helper.php
1 year ago
price_breakdown_helper.php
1 year ago
process_jobs_helper.php
1 year ago
processes_helper.php
1 year ago
replacer_helper.php
1 year ago
resource_helper.php
1 year ago
roles_helper.php
1 year ago
router_helper.php
1 year ago
service_helper.php
1 year ago
sessions_helper.php
1 year ago
settings_helper.php
1 year ago
shortcodes_helper.php
1 year ago
sms_helper.php
1 year ago
steps_helper.php
1 year ago
stripe_connect_helper.php
1 year ago
styles_helper.php
1 year ago
support_topics_helper.php
1 year ago
time_helper.php
1 year ago
timeline_helper.php
1 year ago
transaction_intent_helper.php
1 year ago
util_helper.php
1 year ago
version_specific_updates_helper.php
1 year ago
work_periods_helper.php
1 year ago
wp_datetime.php
1 year ago
wp_user_helper.php
1 year ago
auth_helper.php
286 lines
| 1 | <?php |
| 2 | |
| 3 | class OsAuthHelper { |
| 4 | |
| 5 | public static ?\LatePoint\Misc\User $current_user = null; |
| 6 | public static $logged_in_customer_id = false; |
| 7 | |
| 8 | public static function set_current_user(){ |
| 9 | if(\OsWpUserHelper::is_user_logged_in()){ |
| 10 | // if wp user is logged in - load from it |
| 11 | self::$current_user = \LatePoint\Misc\User::load_from_wp_user(\OsWpUserHelper::get_current_user()); |
| 12 | }else{ |
| 13 | self::$current_user = new \LatePoint\Misc\User(); |
| 14 | } |
| 15 | $customer = self::get_logged_in_customer(); |
| 16 | if($customer) self::$current_user->customer = $customer; |
| 17 | } |
| 18 | |
| 19 | public static function get_current_user(): \LatePoint\Misc\User{ |
| 20 | if(!self::$current_user) self::set_current_user(); |
| 21 | return self::$current_user; |
| 22 | } |
| 23 | |
| 24 | public static function get_highest_current_user_id(){ |
| 25 | $user_id = false; |
| 26 | switch(self::get_highest_current_user_type()){ |
| 27 | case LATEPOINT_USER_TYPE_ADMIN: |
| 28 | case LATEPOINT_USER_TYPE_CUSTOM: |
| 29 | $user_id = self::get_logged_in_wp_user_id(); |
| 30 | break; |
| 31 | case LATEPOINT_USER_TYPE_AGENT: |
| 32 | $user_id = self::get_logged_in_agent_id(); |
| 33 | break; |
| 34 | case LATEPOINT_USER_TYPE_CUSTOMER: |
| 35 | $user_id = self::get_logged_in_customer_id(); |
| 36 | break; |
| 37 | } |
| 38 | return $user_id; |
| 39 | } |
| 40 | |
| 41 | public static function get_admin_or_agent_avatar_url(){ |
| 42 | $avatar_url = LATEPOINT_DEFAULT_AVATAR_URL; |
| 43 | if(self::is_agent_logged_in()){ |
| 44 | $agent = self::get_logged_in_agent(); |
| 45 | $avatar_url = $agent->get_avatar_url(); |
| 46 | }elseif(self::get_logged_in_wp_user_id()){ |
| 47 | $wp_user = self::get_logged_in_wp_user(); |
| 48 | $avatar_url = get_avatar_url($wp_user->user_email); |
| 49 | } |
| 50 | return $avatar_url; |
| 51 | } |
| 52 | |
| 53 | public static function get_highest_current_user_type(){ |
| 54 | // check if WP admin is logged in |
| 55 | $user_type = false; |
| 56 | if(self::get_current_user()->backend_user_type){ |
| 57 | // backend user, admin, agent or custom role |
| 58 | return self::get_current_user()->backend_user_type; |
| 59 | }elseif(self::get_current_user()->customer){ |
| 60 | // customer |
| 61 | return LATEPOINT_USER_TYPE_CUSTOMER; |
| 62 | } |
| 63 | return $user_type; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | public static function login_wp_user($user){ |
| 69 | clean_user_cache($user->ID); |
| 70 | wp_set_current_user($user->ID); |
| 71 | wp_set_auth_cookie($user->ID); |
| 72 | update_user_caches($user); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | public static function login_customer($email, $password){ |
| 77 | if(empty($email) || empty($password)) return false; |
| 78 | $email = sanitize_email($email); |
| 79 | if(self::wp_users_as_customers()){ |
| 80 | $wp_user = wp_signon(['user_login' => $email, 'user_password' => $password]); |
| 81 | if(!is_wp_error($wp_user)){ |
| 82 | // successfully logged into wp user |
| 83 | // check if latepoint customer exists in db for this wp user |
| 84 | wp_set_current_user($wp_user->ID); |
| 85 | $customer = OsCustomerHelper::get_customer_for_wp_user($wp_user); |
| 86 | if($customer->id){ |
| 87 | return $customer; |
| 88 | }else{ |
| 89 | OsDebugHelper::log('Can not login because can not create LatePoint Customer from WP User', 'customer_login_error', $customer->get_error_messages()); |
| 90 | return false; |
| 91 | } |
| 92 | return $customer; |
| 93 | }else{ |
| 94 | return false; |
| 95 | } |
| 96 | }else{ |
| 97 | $customer = new OsCustomerModel(); |
| 98 | $customer = $customer->where(array('email' => $email))->set_limit(1)->get_results_as_models(); |
| 99 | if($customer && OsAuthHelper::verify_password($password, $customer->password)){ |
| 100 | OsAuthHelper::authorize_customer($customer->id); |
| 101 | return $customer; |
| 102 | }else{ |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | public static function wp_users_as_customers(){ |
| 109 | return OsSettingsHelper::is_on('wp_users_as_customers', false); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | // CUSTOMERS |
| 114 | // --------------- |
| 115 | |
| 116 | public static function logout_customer(){ |
| 117 | if(self::wp_users_as_customers()){ |
| 118 | wp_logout(); |
| 119 | }else{ |
| 120 | OsSessionsHelper::destroy_customer_session_cookie(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public static function authorize_customer($customer_id){ |
| 125 | $customer = new OsCustomerModel(); |
| 126 | $customer = $customer->where(['id' => $customer_id])->set_limit(1)->get_results_as_models(); |
| 127 | if(empty($customer)){ |
| 128 | OsDebugHelper::log('Tried to authorize customer with invalid ID', 'customer_authorization', ['customer_id' => $customer_id]); |
| 129 | return false; |
| 130 | } |
| 131 | if(self::wp_users_as_customers()){ |
| 132 | |
| 133 | if($customer->wordpress_user_id){ |
| 134 | $wp_user = get_user_by( 'id', $customer->wordpress_user_id ); |
| 135 | // check if WP User exists, if not - create new one and get ID, otherwise get ID from customer record, since its valid |
| 136 | $wordpress_user_id = ($wp_user) ? $customer->wordpress_user_id : OsCustomerHelper::create_wp_user_for_customer($customer); |
| 137 | }else{ |
| 138 | $wordpress_user_id = OsCustomerHelper::create_wp_user_for_customer($customer); |
| 139 | } |
| 140 | |
| 141 | if($wordpress_user_id){ |
| 142 | $wp_user = get_user_by( 'id', $wordpress_user_id ); |
| 143 | if( $wp_user ) { |
| 144 | self::login_wp_user($wp_user); |
| 145 | }else{ |
| 146 | OsDebugHelper::log('WordPress user ID for customer is not found or can not be created.', 'customer_create_error', ['customer_id' => $customer_id, 'wordpress_user_id' => $wordpress_user_id]); |
| 147 | } |
| 148 | }else{ |
| 149 | OsDebugHelper::log('WordPress user ID for customer is not found or can not be created.', 'customer_create_error', ['customer_id' => $customer_id]); |
| 150 | } |
| 151 | }else{ |
| 152 | OsSessionsHelper::start_or_use_session_for_customer($customer_id); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | public static function get_logged_in_customer_id(){ |
| 157 | if(self::wp_users_as_customers()){ |
| 158 | // using wp users as customers |
| 159 | if(OsWpUserHelper::is_user_logged_in()){ |
| 160 | $wp_user = wp_get_current_user(); |
| 161 | // search connected latepoint customer |
| 162 | $customer = OsCustomerHelper::get_customer_for_wp_user($wp_user); |
| 163 | if($customer->id){ |
| 164 | return $customer->id; |
| 165 | }else{ |
| 166 | OsDebugHelper::log('Can not create LatePoint Customer from WP User', 'customer_create_error', $customer->get_error_messages()); |
| 167 | return false; |
| 168 | } |
| 169 | }else{ |
| 170 | return false; |
| 171 | } |
| 172 | }else{ |
| 173 | if(self::$logged_in_customer_id) return self::$logged_in_customer_id; |
| 174 | $customer_id = OsSessionsHelper::get_customer_id_from_session(); |
| 175 | // make sure customer with this ID exists in database |
| 176 | $customer = new OsCustomerModel($customer_id); |
| 177 | if(!$customer->is_new_record()){ |
| 178 | self::$logged_in_customer_id = $customer_id; |
| 179 | return self::$logged_in_customer_id; |
| 180 | }else{ |
| 181 | // customer not found, destroy this invalid customer ID in session cookie |
| 182 | OsSessionsHelper::destroy_customer_session_cookie(); |
| 183 | return false; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | public static function is_customer_logged_in(){ |
| 189 | return self::get_logged_in_customer_id(); |
| 190 | } |
| 191 | |
| 192 | public static function get_logged_in_customer(){ |
| 193 | $customer = false; |
| 194 | if(self::is_customer_logged_in()){ |
| 195 | $customer = new OsCustomerModel(self::get_logged_in_customer_id()); |
| 196 | if($customer->is_new_record()) $customer = false; |
| 197 | } |
| 198 | return $customer; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | // AGENTS |
| 203 | // ------------- |
| 204 | |
| 205 | public static function get_logged_in_agent_id(){ |
| 206 | $agent_id = false; |
| 207 | if(self::is_agent_logged_in()){ |
| 208 | if(self::get_current_user()->agent && self::get_current_user()->agent->id) $agent_id = self::get_current_user()->agent->id; |
| 209 | } |
| 210 | return $agent_id; |
| 211 | } |
| 212 | |
| 213 | public static function is_agent_logged_in(){ |
| 214 | return (self::get_current_user()->backend_user_type == LATEPOINT_USER_TYPE_AGENT); |
| 215 | } |
| 216 | |
| 217 | public static function get_logged_in_agent(){ |
| 218 | $agent = false; |
| 219 | if(self::is_agent_logged_in()){ |
| 220 | $agent = new OsAgentModel(); |
| 221 | $agent = $agent->where(['wp_user_id' => self::get_logged_in_wp_user_id()])->set_limit(1)->get_results_as_models(); |
| 222 | } |
| 223 | return $agent; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | public static function is_custom_backend_user_logged_in(){ |
| 228 | return (self::get_current_user()->backend_user_type == LATEPOINT_USER_TYPE_CUSTOM); |
| 229 | } |
| 230 | |
| 231 | public static function is_admin_logged_in(){ |
| 232 | return (self::get_current_user()->backend_user_type == LATEPOINT_USER_TYPE_ADMIN); |
| 233 | } |
| 234 | |
| 235 | public static function get_logged_in_admin_user(){ |
| 236 | $admin_user = false; |
| 237 | if(self::is_admin_logged_in()){ |
| 238 | $admin_user = self::get_logged_in_wp_user(); |
| 239 | } |
| 240 | return $admin_user; |
| 241 | } |
| 242 | |
| 243 | public static function get_logged_in_admin_user_id(){ |
| 244 | $admin_id = false; |
| 245 | if(self::is_admin_logged_in()){ |
| 246 | $admin_id = self::get_logged_in_wp_user_id(); |
| 247 | } |
| 248 | return $admin_id; |
| 249 | } |
| 250 | |
| 251 | public static function get_logged_in_custom_user_id(){ |
| 252 | $admin_id = false; |
| 253 | if(self::is_custom_backend_user_logged_in()){ |
| 254 | $admin_id = self::get_logged_in_wp_user_id(); |
| 255 | } |
| 256 | return $admin_id; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | // WP USER |
| 267 | public static function get_logged_in_wp_user_id(){ |
| 268 | return OsWpUserHelper::get_current_user_id(); |
| 269 | } |
| 270 | |
| 271 | public static function get_logged_in_wp_user(){ |
| 272 | return OsWpUserHelper::get_current_user(); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | // UTILS |
| 277 | |
| 278 | public static function hash_password($password){ |
| 279 | return password_hash($password, PASSWORD_DEFAULT); |
| 280 | } |
| 281 | |
| 282 | public static function verify_password($password, $hash){ |
| 283 | return password_verify($password, $hash); |
| 284 | } |
| 285 | |
| 286 | } |