Comments (14)
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.
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;
}`
from laravel-crm.
Part 2 of the teams enabled bug
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.
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.
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.
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.
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.
(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.
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.
from laravel-crm.
from laravel-crm.
from laravel-crm.
https://github.com/Lemnocity/clean-crm here is the link to our clean crm project
from laravel-crm.
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)
- Unable to install Package HOT 11
- Proper contribution through local package HOT 3
- Feature enhancements HOT 4
- Organization name is being encrypted on creation. HOT 3
- Feature enhancement HOT 4
- 403 forbidden after login HOT 3
- CRM_Team destroy 403 THIS ACTION IS UNAUTHORIZED. HOT 3
- Font awesome not loading correctly HOT 2
- Hardcoded current_crm_team_id
- Errors occurs when ran composer test HOT 2
- Spain translate?? HOT 1
- Problem with the views, controller and routes HOT 1
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cncerp.crm_users' doesn't exist (Connection: mysql, SQL: alter table `crm_users` add `url` varchar(255) null) HOT 3
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel_crm.product_attributes' doesn't exist HOT 1
- Convert lead to deal - Link Product to Deal Error
- Deals does not store into DB the name of a customer HOT 3
- Feature : Customer History ? HOT 2
- Custom fields are not displaying to any model add or edit HOT 2
- Relation between People, Customers, Organizations HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-crm.