PHP code example of madbox-99 / laravel-user-team-sync

1. Go to this page and download the library: Download madbox-99/laravel-user-team-sync 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/ */

    

madbox-99 / laravel-user-team-sync example snippets


'publisher' => [
    'app_source' => 'config', // default
    'api_key' => env('USER_TEAM_SYNC_API_KEY'),
    'apps' => [
        'crm' => [
            'url' => env('CRM_APP_URL'),
            'api_key' => env('CRM_APP_API_KEY'), // optional, falls back to default
            'active' => true,
        ],
        'shop' => [
            'url' => env('SHOP_APP_URL'),
            'api_key' => env('SHOP_APP_API_KEY'),
            'active' => true,
        ],
    ],
],

use Madbox99\UserTeamSync\Models\SyncApp;

// Add a new app
SyncApp::create([
    'name' => 'crm',
    'url' => 'https://crm.example.com',
    'api_key' => 'secret-key', // stored encrypted
    'is_active' => true,
]);

// Deactivate an app
SyncApp::where('name', 'crm')->update(['is_active' => false]);

'receiver' => [
    'api_key' => env('USER_TEAM_SYNC_API_KEY'),
    'route_prefix' => 'api',
    'role_driver' => 'spatie',
    'default_role' => 'subscriber',
    'default_active' => false,
],

'client' => [
    // This app's own key. Must equal sync_apps.name on the publisher and the
    // slug of the plan category that grants access to this app. A login is
    // refused when this key is absent from the token's `apps` claim.
    'app_key' => env('IDENTITY_APP_KEY'),

    'identity_url' => env('IDENTITY_URL'),
    'client_id' => env('IDENTITY_CLIENT_ID'),
    'client_secret' => env('IDENTITY_CLIENT_SECRET'),
    'redirect_uri' => env('IDENTITY_REDIRECT_URI'),
    'scopes' => '',
    'http_timeout' => env('IDENTITY_HTTP_TIMEOUT', 10),

    // Kept short: the revalidation middleware runs on every authenticated
    // page, so a hanging connect would otherwise pin a worker and the
    // session lock for the full read timeout, request after request.
    'http_connect_timeout' => env('IDENTITY_HTTP_CONNECT_TIMEOUT', 3),

    // Re-fetch the claims once the session's last check is older than this.
    'revalidate_after_minutes' => env('IDENTITY_REVALIDATE_MINUTES', 15),

    // How long a session survives while the identity provider is unreachable.
    'grace_hours' => env('IDENTITY_GRACE_HOURS', 24),

    // How long to wait before retrying an unreachable provider. Without it
    // every request would retry, so a slow provider would cost every page
    // load the full HTTP timeout for the whole grace window.
    'retry_after_minutes' => env('IDENTITY_RETRY_MINUTES', 5),

    // Comma-separated e-mails. Non-empty during a phased rollout: only these
    // users go through SSO, everyone else keeps using the legacy login and
    // the legacy push. Empty means everyone goes through SSO.
    'allowlist' => array_values(array_filter(array_map('trim', explode(',', (string) env('IDENTITY_SSO_ALLOWLIST', ''))))),

    // Keeps the legacy receiver endpoints mounted while both worlds run
    // side by side. Turn off once the rollout is complete.
    'legacy_receiver' => env('IDENTITY_LEGACY_RECEIVER', true),

    // Maps a token role name onto a local role name. Honoured on both role
    // drivers — see "Translating roles" below.
    'role_map' => [],

    // Where to send a user who authenticated but has no subscription
    // covering this app.
    'subscribe_url' => env('IDENTITY_SUBSCRIBE_URL'),

    // Where to send a user back to retry signing in after a failed or
    // refused callback. Defaults to this app's own login page, since a
    // phased rollout keeps the legacy password form available as a
    // fallback for exactly this case.
    'login_url' => env('IDENTITY_LOGIN_URL', '/login'),
],

'receiver' => ['role_driver' => 'default', 'default_role' => 'viewer'],

'client' => [
    'role_map' => [
        'admin' => 'admin',
        'manager' => 'technician',
        'subscriber' => 'viewer',
    ],
],

'receiver' => [
    'local_only_roles' => ['Super-Admin'],
],

->authMiddleware([
    Authenticate::class,
    \Madbox99\UserTeamSync\Client\Http\Middleware\RevalidateIdentity::class,
])

// This automatically triggers sync to all receiver apps
$user->update(['role' => 'admin']);

use Madbox99\UserTeamSync\Facades\UserTeamSync;

// Create a user on all receiver apps
UserTeamSync::createUser(
    email: '[email protected]',
    name: 'John Doe',
    password: 'plain-text-password', // hashed automatically before sending
    role: 'editor',
    ownerEmail: '[email protected]',
);

// Sync user changes
UserTeamSync::syncUser('[email protected]', [
    'new_email' => '[email protected]',
    'role' => 'admin',
]);

// Create a team on all receiver apps
UserTeamSync::createTeam(
    teamName: 'Marketing',
    userEmail: '[email protected]',
    slug: 'marketing', // optional, auto-generated from name
);

// Toggle user active status on a specific app
UserTeamSync::toggleUserActive(
    userEmail: '[email protected]',
    isActive: true,
    appKey: 'crm',
);

UserTeamSync::removeTeamMember(
    userEmail: '[email protected]',
    teamSlug: 'acme',
    appKey: 'crm',
    userUuid: $user->uuid,   // optional
    teamUuid: $team->uuid,   // optional
);

// In EventServiceProvider or listener
use Madbox99\UserTeamSync\Events\UserCreatedFromSync;

class HandleSyncedUser
{
    public function handle(UserCreatedFromSync $event): void
    {
        // $event->user
    }
}

'logging' => [
    'enabled' => true,
    'table' => 'sync_logs',
    'retention_days' => 30,
],

use Madbox99\UserTeamSync\Concerns\HasSuperAdmin;

class User extends Authenticatable
{
    use HasSuperAdmin; // isSuperAdmin(), setSuperAdmin(), grantSuperAdmin(), revokeSuperAdmin(), superAdmins()
}

use Madbox99\UserTeamSync\Filament\SuperAdminColumn;
use Madbox99\UserTeamSync\Filament\SuperAdminToggle;

SuperAdminToggle::make(),   // form
SuperAdminColumn::make(),   // table
bash
php artisan user-team-sync:install
bash
php artisan vendor:publish --tag=user-team-sync-config
php artisan vendor:publish --tag=user-team-sync-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=user-team-sync-super-admin   # adds users.is_super_admin
php artisan migrate
php artisan user-team-sync:super-admin [email protected]       # the first one; --revoke to remove