PHP code example of jurager / teams

1. Go to this page and download the library: Download jurager/teams library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jurager / teams example snippets


 

namespace App\Providers;

use Jurager\Teams\Traits\HasTeams;

class User extends Model {

    use HasTeams;
}

 

namespace App\Providers;

class AuthServiceProvider extends ServiceProvider
{
    protected $policies = [
        \Jurager\Teams\Models\Team::class => \App\Policies\TeamPolicy::class,
    ];
}

// Access the team's owner...
$team->owner

// Get all the team's users, excluding owner
$team->users()

// Get all the team's users, including the owner...
$team->allUsers()

// Determine if the given user is a team member...
$team->hasUser((object) $user)

// Get all the abilities belong to the team.
$team->abilities()

// Get all the team's roles.
$team->roles()

// Get the role from the team by role id or name 
$team->findRole((int|string) $id)

// Return the user role object from the team
$team->userRole((object) $user)

// Add new role to the team
$team->addRole((string) $name, (array) $capabilities)

// Update the role in the team
$team->updateRole((string) $name, (array) $capabilities)

// Deletes the given role from team
$team->deleteRole((string) $name)

// Get all groups of the team.
$team->groups()

// Get team group by its code
$team->group((string) $code)

// Add new group to the team
$team->addGroup((string) $code, (string) $name)

// Delete group from the team
$team->deleteGroup((string) $code)

// Determine if the team has a member with the given email address...
$team->hasUserWithEmail((array) $emailAddress)

// Determine if the given user is a team member with the given permission...
$team->userHasPermission((object) $user, (string|array) $permission, (bool) $

// Access the team's that a user belongs to...
$user->teams : Illuminate\Database\Eloquent\Collection

// Access all of a user's owned teams...
$user->ownedTeams : Illuminate\Database\Eloquent\Collection

// Access all the team's (including owned teams) that a user belongs to...
$user->allTeams() : Illuminate\Database\Eloquent\Collection

// Determine if a user owns a given team...
$user->ownsTeam((object) $team) : bool

// Determine if a user belongs to a given team...
$user->belongsToTeam((object) $team) : bool

// Get the role that the user is assigned on the team...
$user->teamRole((object) $team) : \Jurager\Teams\Role

// Determine if the user has the given role on the given team...
$user->hasTeamRole((object) $team, (string|array) 'admin', (bool) $// Forbid an ability for user to action on certain model, used in case if global permission or role allowing this action
$user->forbidTeamAbility((object) $team, (string) 'server:edit', (object) $server) : bool

// Add new group to the team
$team->addGroup((string) $code, (string) $name)

// Delete group from the team
$team->deleteGroup((string) $code)

// Get all groups of the team.
$team->groups();

// Get team group by its code
$team->group((string) $code);

// Get all group users
$team->group((string) $code)->users();

// Attach users or user to a group
$team->group((string) $code)->attachUser((Collection|Model) $user);

// Detach users or user from group
$team->group((string) $code)->detachUser((Collection|Model) $user);

// Add an ability for user to action on certain model within team group, if permission is not found, will create a new one
$user->allowTeamAbility((object) $team, (string) 'server:edit', (object) $server, (object|null) $group));

// Forbid an ability for user to action on certain model within team group
$user->forbidTeamAbility((object) $team, (string) 'server:edit', (object) $server, (object|null) $group);

// Delete user ability to action on certain model within team group
$user->deleteTeamAbility((object) $team, (string) 'server:edit', (object) $server, (object|null) $group);

// Determinate if user can perform an action
$user->hasTeamAbility((object) $team, (string) 'server:edit', (object) $server)

$team = new Team();

$team->name = 'Example Team';
$team->code = 'example_team';

if ($team->save()) {

    $team->addRole('admin', [
        'employees.*',
        'sections.*',
        'articles.*',
        'tags.*',
        'comments.*',
        'team.edit',
        'stores.*',
        'plan.edit',
    ]);
    
    $team->addRole('user', [
        'employees.view',
        'articles.view',
        'articles.add',
        'sections.view',
        'sections.add',
        'comments.add',
        'tags.view',
        'stores.add',
        'stores.delete',
        'tags.add',
    ]);
}

return $user->hasTeamPermission((string) $server->team, (string) 'server:update');

User::allowTeamAbility((object) $team, string 'edit', (object) $article, (object|null) $group);

User::hasTeamAbility((object) $team, string 'edit', (object) $article);

User::forbidTeamAbility((object) $team, (string) 'edit', (object) $article, (object|null) $group);

Ability::firstOrCreate([ 'name' => 'edit', 'title' => 'Edit' ]);

Route::group(['prefix' => 'admin', 'middleware' => ['role:admin,team_id']], function() {
    Route::get('/users', 'UserController@usersIndex');
    Route::get('/user/edit', ['middleware' => ['permission:edit-users,team_id'], 'uses' => 'UserController@userEdit']);
});

Route::get('/{team_id}/users', ['middleware' => ['permission:views-users'], 'uses' => 'CommonController@commonUsers']);

'middleware' => ['role:admin|root,team_id']

'middleware' => ['role:admin|root,team_id']
// $user->hasTeamRole($team, ['admin', 'root']);

'middleware' => ['permission:edit-post|edit-user']
// $user->hasTeamPermission($team, ['edit-post', 'edit-user']);

'middleware' => ['role:admin|root,team_id,oot'], r,team_id,r'], 

'middleware' => ['ability:edit,App\Models\Article,atricle_id']
// $user->hasTeamAbility($team, 'edit', $article);
sh
php artisan teams:install
sh
php artisan migrate