%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/camillo/camillo-api-master/app/Nova/Actions/
Upload File :
Create Path :
Current File : /var/www/html/camillo/camillo-api-master/app/Nova/Actions/PrincipalResetPassword.php

<?php

namespace App\Nova\Actions;

use App\Enums\UsertypeEnum;
use App\Mail\PrincipalResetPasswordEmail;
use App\Services\SmsService;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Laravel\Nova\Actions\Action;
use Illuminate\Support\Collection;
use Laravel\Nova\Fields\ActionFields;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;


class PrincipalResetPassword extends Action
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * The displayable name of the action.
     *
     * @var string
     * @return array|null|string
     */
    public function name(){
        return __('Reset Principal Password');
    }
    
    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields $fields
     * @param  \Illuminate\Support\Collection $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
        foreach($models as $model){
            $backend_users = [UsertypeEnum::PRINCIPAL, UsertypeEnum::ADMIN, UsertypeEnum::TEACHER];

            //generate random unique token
            $hash= Hash::make($model->fiscal_code . time());
            $token = str_replace(["$","#","%",".",",","@","/","\\"],rand(0,9), $hash);

            //save remember token in user table
            $user = $model;
            $user->remember_token = $token;
            $user->active = false;

            if (!$user->save()){
                return Action::danger(__('Could not save token!'));
            }

            if(in_array($user->usertype->role, $backend_users)) {
                //Send E-mail
                if ($user->usertype->role === UsertypeEnum::TEACHER) {
                    $url = env("WEBAPP_BASE_URL", "https://app-staging.camillo.online") . "/confirm?token=" . $token;
                }
                else {
                    $url = env(
                            'API_BASE_URL', "https://bo-staging.camillo.online"
                        ) . "/principal/reset?token=" . $token;
                }
                try {
                    Mail::to($user->individual->email)->send(new PrincipalResetPasswordEmail($url));
                } catch (\Exception $exception) {
                    //return Action::danger($exception->getMessage());

                    return Action::danger(__('Something went wrong!'));
                }

            } else {
                //Send SMS
                $type = "password_reset";
                $url = env("WEBAPP_BASE_URL","https://app-staging.camillo.online") . "/confirm?token=" . $token;
                $sms = new SmsService($model->individual, $type, $url);
                if(!$sms->send_invitation()){
                    return Action::danger(__('Something went wrong!'));
                }


            }

            return Action::message(__('reset_link_sent'));
        }
    }

    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        return [];
    }
}

Zerion Mini Shell 1.0