Git Product home page Git Product logo

Comments (14)

DCox2016 avatar DCox2016 commented on June 11, 2024

To fix this issue, I had to add a check to the LaravelCrmInstall.php file
`
if ($user = \App\User::where('email', $email)->first()) {
$this->info('User already exists, granting crm access...');

            $team = null;  // Initialize the $team variable
        
            // Check if LARAVEL_CRM_TEAMS key in .env is set to true
            if (env('LARAVEL_CRM_TEAMS', false)) {
                $team = Team::firstOrCreate(['name' => 'Owner','user_id' => $user->id, 'team_id' => 1]);
                $user->current_crm_team_id = $team->id;  // Associate the user with the team
                $user->save();
            }
        
            $user->update([
                'crm_access' => 1,
            ]);
        
            if (!$team) {
                if (!$user->hasRole('Owner')) {
                    $user->assignRole('Owner');
                }
            } else {
                if (!$user->hasRole('Owner')) {
                    $user->assignRole('Owner', $team);
                }
            }
        
            $this->info('User access and role updated.');
        } else {
            $user = \App\User::forceCreate([
                'name' => trim($firstname.' '.$lastname),
                'email' => $email,
                'password' => Hash::make($password),
                'crm_access' => 1,
            ]);
        
            $team = null;  // Initialize the $team variable
        
            // Check if LARAVEL_CRM_TEAMS key in .env is set to true
            if (env('LARAVEL_CRM_TEAMS', false)) {
                $team = Team::firstOrCreate(['name' => 'Owner','user_id' => $user->id, 'team_id' => 1]);
                $user->current_crm_team_id = $team->id;  // Associate the user with the team
                $user->save();
            }
        
            if ($team) {
                $user->assignRole('Owner', $team);
            } else {
                $user->assignRole('Owner');
            }
        
            $this->info('User created with owner role');
        }`

from laravel-crm.

DCox2016 avatar DCox2016 commented on June 11, 2024

I also had to edit Spatie's HasRole trait function assignRole() to accept a team object. As of now the function only accepts a role but it teams is enabled and you don't pass a team object you will get the error above.
assignRole()
` public function assignRole($roles, $team = null)
{
// Ensure roles is always an array
if (!is_array($roles)) {
$roles = [$roles];
}

    $roles = collect($roles)
        ->reduce(function ($array, $role) use ($team) {
            $role = $this->getStoredRole($role);
            if (! $role instanceof Role) {
                return $array;
            }

            $this->ensureModelSharesGuard($role);

            $array[$role->getKey()] = PermissionRegistrar::$teams && ! is_a($this, Permission::class) ?
                [PermissionRegistrar::$teamsKey => ($team ? $team->id : null)] : [];

            return $array;
        }, []);

    $model = $this->getModel();

    if ($model->exists) {
        $this->roles()->sync($roles, false);
        $model->load('roles');
    } else {
        $class = \get_class($model);
        $class::saved(function ($object) use ($roles, $model) {
            if ($model->getKey() != $object->getKey()) {
                return;
            }
            $model->roles()->sync($roles, false);
            $model->load('roles');
        });
    }

    if (is_a($this, get_class($this->getPermissionClass()))) {
        $this->forgetCachedPermissions();
    }

    return $this;
}`
image

from laravel-crm.

DCox2016 avatar DCox2016 commented on June 11, 2024

Part 2 of the teams enabled bug
image
This a user has has rights to view everything, but the nav menu items are not displaying. This is because we have enabled teams which is a user -> teams -> permissions model so all the @can and the middlewares do not work.

from laravel-crm.

DCox2016 avatar DCox2016 commented on June 11, 2024

Steps we did to fix this issue

  • After login set users session team_id to there assigned id.
  • Make a Teams model that extends the VentureDrake Team model
    Set the default gaurd_name = 'web'
    *Eloquent was not work correctly so I made the permissions function
   public function permissions(): BelongsToMany
    {
        return $this->morphToMany(
            config('permission.models.permission'),
            'model',
            config('permission.table_names.model_has_permissions'),
            config('permission.column_names.model_morph_key'),
            'permission_id'
        )
        ->withPivot('team_id')
        ->wherePivot('team_id', $this->id)
        ->as('access');
    }
  • Make a TeamPermissionsController use VentureDrake\LaravelCrm\Models\Permission and App\Models\Teams.
    
    {
        $allTeams = Teams::all()->groupBy('team_id')->map(function ($teamGroup) {
            return $teamGroup->first();
        });
        $allPermissions = Permission::all();
        return view('admin.dashboard.teams_permissions', compact('team', 'allPermissions', 'allTeams'));
    }

    public function update(Request $request, Teams $team)
    {
        $currentPermissions = $team->permissions->pluck('id')->toArray();

        // Get the desired permissions from the request.
        $desiredPermissions = $request->input('permissions');

        foreach ($currentPermissions as $permissionId) {
            if (!in_array($permissionId, $desiredPermissions)) {
                $team->permissions()->detach($permissionId);
            }
        }

        foreach ($desiredPermissions as $permissionId) {
            if (!in_array($permissionId, $currentPermissions)) {
                $team->permissions()->attach($permissionId, ['team_id' => $team->id]);
            }
        }

        return redirect()->route('teams_permission.edit', $team)->with('success', 'Permissions updated successfully');
    }
    ```

from laravel-crm.

DCox2016 avatar DCox2016 commented on June 11, 2024

Create middleware

  • app/Http/Middleware/CheckCustomPermission.php
    `namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class CheckCustomPermission
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle($request, Closure $next, $action = null, $model = null)
{
$user = $request->user();

    $hasPermission = false;

    if ($action && $model) {
        $hasPermission = $user->can($action, $model);
    } elseif ($action) {
        $hasPermission = $user->can($action);
    }
    if (!$hasPermission) {
        abort(403, 'Unauthorized action.');
    }

    return $next($request);
}    

}
`

Replace
'can' => \Illuminate\Auth\Middleware\Authorize::class,
in app\Http\Kernel.php protected $middlewareAliases with
'can' => \App\Http\Middleware\CheckCustomPermission::class,

from laravel-crm.

DCox2016 avatar DCox2016 commented on June 11, 2024

Edit User Model
`use VentureDrake\LaravelCrm\Models\Team;

public function team()
{
return $this->belongsTo(Team::class);
}

    public function canForTeam($permission, $teamId)
{

    $team = Teams::find($teamId);

    if ($this->team_id == $teamId) {

        $teamPermissions = $team->permissions->pluck('name')->toArray();

        if (in_array($permission, $teamPermissions)) {
            return true;
        }
    }

    return false;
}

`

from laravel-crm.

DCox2016 avatar DCox2016 commented on June 11, 2024

Update app/Providers/AuthServiceProvider.php
` public function boot()
{
$this->registerPolicies();

    // List of permissions
    $permissions = \DB::table('permissions')->pluck('name');

    foreach ($permissions as $permission) {
        Gate::define($permission, function ($user) use ($permission) {
            // Direct permission check
            if ($user->getPermissionsViaRoles()->contains($permission)) {
                return true;
            }

            // Team permission check
            if ($user->canForTeam($permission, $user->team_id)) {
               return true;
            }

            return false;
        });
    }`

from laravel-crm.

DCox2016 avatar DCox2016 commented on June 11, 2024

(config('laravel-crm.teams') && auth()->user()->currentTeam && auth()->user()->currentTeam->user_id == auth()->user()->id

currentTeam function was not working so I update it like so
elseif (config('laravel-crm.teams') && auth()->user()->team->user_id == auth()->user()->id && ! auth()->user()->hasRole('Owner')) {

and on the where I replace it
'team_id' => auth()->user()->team->user_id,

from laravel-crm.

DCox2016 avatar DCox2016 commented on June 11, 2024

For the middle ware to work each one needs to check
if (Env::get('LARAVEL_CRM_TEAMS')) {
return $user->canForTeam('view crm fields', $user->team_id);
}

from laravel-crm.

DCox2016 avatar DCox2016 commented on June 11, 2024
image once you attach permissions to a team you will start seeing the nav bar image

from laravel-crm.

DCox2016 avatar DCox2016 commented on June 11, 2024
image image

from laravel-crm.

DCox2016 avatar DCox2016 commented on June 11, 2024
image by adding this to you policy you don't have to update your routes image I am going to post this to our clean crm project. It is public. Also, because our team_id id team_id on the user you will need to update your to what every you made it $user->current_crm_team_id the clean project as you can see if current_crm_team_id

from laravel-crm.

DCox2016 avatar DCox2016 commented on June 11, 2024

https://github.com/Lemnocity/clean-crm here is the link to our clean crm project

from laravel-crm.

andrewdrake avatar andrewdrake commented on June 11, 2024

Thanks for reporting this. So there are a few issues here.

I built the teams function basically to use with Laravel Jetstream, which has a Teams feature. This is then used to create multi-tenant applications. So as it stands right now you need to be using Jetstream.

I have this working myself in a few projects, however what we need to do is allow for projects that don't use Jetstream. And also I should probably create a few variations of the starter project to make use of Breeze vs Jetstream vs Custom.

Also you have made me aware that the installer will not work with teams, I will need to update that.

Now I think this might be a little confusing, but the "Teams" in the CRM are not the same thing as "teams" in Jetstream. Teams in the CRM are teams of people, say "Sales Team", "Accounts Team", etc

Where as Teams in Jetstream, are seperate tenants in the project. "Company A", "Company B", etc

from laravel-crm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.