Skip to content

Commit

Permalink
Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
acacha committed Jul 13, 2017
1 parent 413e7c4 commit 13a6e1e
Show file tree
Hide file tree
Showing 14 changed files with 616 additions and 7 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -23,6 +23,7 @@
"acacha/stateful-eloquent": "^0.1.3",
"laravel/passport": "^2.0",
"predis/predis": "^1.1",
"pulkitjalan/google-apiclient": "^3.1",
"scool/ebre_escool_model": "dev-master",
"spatie/laravel-permission": "^2.1",
"venturecraft/revisionable": "^1.28"
Expand Down
56 changes: 54 additions & 2 deletions config/users.php
Expand Up @@ -26,16 +26,27 @@

/*
|--------------------------------------------------------------------------
| Register Acacha Stateful Service Provider?
| Register Acacha Stateful Service Provider?
|--------------------------------------------------------------------------
| Set this value to true to register Acacha\Stateful\Providers\StatefulServiceProvider.
| Set this value to true to register Acacha\Stateful\Providers\StatefulServiceProvider.
| Remember to manually register the service provider in your config/app.php file if
| this value is set to false.
|
*/

'register_acacha_stateful_service_provider' => true,

/*
|--------------------------------------------------------------------------
| Register Google Service Provider?
|--------------------------------------------------------------------------
| Set this value to true to register PulkitJalan\Google\GoogleServiceProvider::class.
| Remember to manually register the service provider in your config/app.php file if
| this value is set to false and you want Google API support.
|
*/
'register_google_service_provider' => true,

/*
|-------------------------------------------------------------------------------------------------
| Register Acacha Stateful Service Provider?
Expand All @@ -58,4 +69,45 @@

'source_database_connection_name' => 'ebre_escool',

/*
|-------------------------------------------------------------------------------------------------
| Google apps/Suite domain.
|-------------------------------------------------------------------------------------------------
| The Google Apps/Suite domain.
|
*/

'google_apps_domain' => env('GOOGLE_APPS_DOMAIN', 'iesebre.com'),

/*
|-------------------------------------------------------------------------------------------------
| Google apps admin user email
|-------------------------------------------------------------------------------------------------
| The Google Apps admin user email used to access to Google Apps.
|
*/

'google_apps_admin_user_email' => env('GOOGLE_APPS_ADMIN_USER_EMAIL', 'sergitur@iesebre.com'),

/*
|-------------------------------------------------------------------------------------------------
| Google apps user email used to check connection to Google Apps.
|-------------------------------------------------------------------------------------------------
| The Google Apps user email used to check connection to Google Apps.
|
*/

'google_apps_user_email_to_check_connection' =>
env('GOOGLE_APPS_USER_EMAIL_TO_CHECK_CONNECTION', 'stur@iesebre.com'),

/*
|-------------------------------------------------------------------------------------------------
| Google apps users per page.
|-------------------------------------------------------------------------------------------------
| Google apps default value for users for page when paginating.
|
*/

'google_apps_users_per_page' => 15,

];
@@ -0,0 +1,56 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

/**
* Class CreateProgressBatchesTable.
*/
class CreateGoogleUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('google_users', function (Blueprint $table) {
$table->increments('id');
$table->string('customerId');
$table->string('kind');
$table->integer('google_id')->unsigned()->nullable();
$table->string('etag');
$table->string('primaryEmail')->unique();
$table->string('givenName');
$table->string('familyName');
$table->string('fullName');
$table->string('orgUnitPath');
$table->json('organizations')->nullable();
$table->boolean('isAdmin');
$table->boolean('isDelegatedAdmin');
$table->dateTime('lastLoginTime');
$table->dateTime('creationTime');
$table->dateTime('deletionTime')->nullable();
$table->dateTime('agreedToTerms');
$table->string('password')->nullable();
$table->string('hashFunction')->nullable();
$table->boolean('suspended');
$table->string('suspensionReason')->nullable();
$table->boolean('changePasswordAtNextLogin');
$table->json('emails');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('google_users');
}
}
17 changes: 17 additions & 0 deletions resources/views/googleApps/google.blade.php
@@ -0,0 +1,17 @@
@extends('adminlte::page')

@section('htmlheader_title')
Google Apps Users Management
@endsection

@section('main-content')
<div class="row">
<div class="col-md-12">

<google-apps-dashboard user-admin="{{ config('users.google_apps_admin_user_email') }}"></google-apps-dashboard>

<google-apps-users-list api-url="{{ env('APP_URL') . env('API_URI','/api/v1/management/users') }}"></google-apps-users-list>

</div>
</div>
@endsection
220 changes: 220 additions & 0 deletions src/Http/Controllers/GoogleAppsUsersController.php
@@ -0,0 +1,220 @@
<?php

namespace Acacha\Users\Http\Controllers;

use Acacha\Users\Services\GoogleApps\GoogleAppsService;

use Acacha\Users\Traits\HasPaginator;
use Google;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

/**
* Class GoogleAppsUsersController.
*
* @package Acacha\Users\Http\Controllers
*/
class GoogleAppsUsersController extends Controller
{

use HasPaginator;

/**
* Google apps service.
*
* @var
*/
public $service;

/**
* GoogleAppsUsersController constructor.
*
* @param $service
*/
public function __construct(GoogleAppsService $service)
{
$this->service = $service;
}


/**
* Show Google apps users.
*/
public function index()
{
$this->authorize('see-google-apps-users');
$data = [];

return view('acacha_users::googleApps.google', $data);
}

/**
* Sync local database from Google Apps/Suite data.
*/
public function localSync(Request $request)
{
$this->authorize('sync-google-apps-users');

return $this->service->localSync();
}

/**
* List Google apps users.
*/
public function all(Request $request)
{
$this->authorize('list-google-apps-users');

$perPage = config('users.google_apps_users_per_page' , 15);
if ($request->has('perPage')) {
$perPage = $request->input('perPage');
}

$users = $this->service->getUsers($perPage);

return $this->paginate($users, $perPage);
}

/**
* Check google apps connection.
*
* @return array
*/
public function check()
{
$this->authorize('check-google-apps-connection');

return $this->service->getConnectionState();
}

public function google3()
{
$directory = Google::make('directory');
dump($directory->users);
}

public function google5()
{
//List all users: https://developers.google.com/apps-script/advanced/admin-sdk-directory

// page = AdminDirectory.Users.list({
// domain: 'example.com',
// orderBy: 'givenName',
// maxResults: 100,
// pageToken: pageToken
// });
}

public function google4()
{
//Example list 100 users
$directory = Google::make('directory');
dump(get_class($directory->users));
dump($directory);
try {
$r = $directory->users->listUsers([
'domain' => 'iesebre.com',
'maxResults' => 500
]);
dump($r);
} catch (\Exception $e) {
dd($e);
}
}

public function google7()
{
//Example list 500 users. max results maxim value is 500
$directory = Google::make('directory');
dump(get_class($directory->users));
try {
$r = $directory->users->listUsers([
'domain' => 'iesebre.com',
'maxResults' => 500
]);
dump($r);
} catch (\Exception $e) {
dd($e);
}
}

public function google8()
{
// Count total number of users
$directory = Google::make('directory');
$count = 0;
$pageToken = null;
do {
try {
$r = $directory->users->listUsers([
'domain' => 'iesebre.com',
'maxResults' => 500,
'pageToken' => $pageToken
]);
$pageToken = $r->nextPageToken;
// dump($pageToken);
// dump($r);
// dump($r->users);
$count = $count + count($r->users);
dump('count: ' . $count);
} catch (\Exception $e) {
dd($e);
}
} while ($pageToken);

dd($count);
}

public function google10()
{
return $this->service->localSync();
}

public function esborrar()
{
$googleClient = Google::getClient();
// dd($googleClient);
$directory = Google::make('directory');
// dd($directory);

$email = "stur@iesebre.com";
try {
$r = $directory->users->get($email);
if($r) {
echo "Name: ".$r->name->fullName."<br/>";
echo "Suspended?: ".(($r->suspended === true) ? 'Yes' : 'No')."<br/>";
echo "Org/Unit/Path: ".$r->orgUnitPath."<br/>";
} else {
echo "User does not exist: $email<br/>";
}
} catch (\Exception $e) {
dd('exception!!!');
}

// if the user doesn't exist, it's safe to create the new user

// $user_to_impersonate = 'adminaccount@example.com';
// $scopes = array('https://www.googleapis.com/auth/admin.directory.user');
// $cred = new Google_Auth_AssertionCredentials(
// $service_account_name,
// $scopes,
// $key,
// 'notasecret', // Default P12 password
// 'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
// $user_to_impersonate
// );

//$dir = new Google_Service_Directory($client);
//$email = "possiblities@example.com";
//$r = $dir->users->get($email);
//if($r) {
// echo "Name: ".$r->name->fullName."<br/>";
// echo "Suspended?: ".(($r->suspended === true) ? 'Yes' : 'No')."<br/>";
// echo "Org/Unit/Path: ".$r->orgUnitPath."<br/>";
//} else {
// echo "User does not exist: $email<br/>";
// // if the user doesn't exist, it's safe to create the new user
//}
}

}

0 comments on commit 13a6e1e

Please sign in to comment.