| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Acacha\Users\Http\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Acacha\Users\Services\GoogleApps\GoogleAppsService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Acacha\Users\Traits\HasPaginator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Google; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Pagination\LengthAwarePaginator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class GoogleAppsUsersController. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @package Acacha\Users\Http\Controllers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class GoogleAppsUsersController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     use HasPaginator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Google apps service. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public $service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * GoogleAppsUsersController constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param $service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct(GoogleAppsService $service) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->service = $service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Show Google apps users. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function index() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->authorize('see-google-apps-users'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return view('acacha_users::googleApps.google', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * Sync local database from Google Apps/Suite data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function localSync(Request $request) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->authorize('sync-google-apps-users'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return $this->service->localSync(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * List Google apps users. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function all(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->authorize('list-google-apps-users'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $perPage = config('users.google_apps_users_per_page' , 15); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if ($request->has('perPage')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $perPage = $request->input('perPage'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $users = $this->service->getUsers($perPage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $this->paginate($users, $perPage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * Check google apps connection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function check() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->authorize('check-google-apps-connection'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return $this->service->getConnectionState(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function google3() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $directory = Google::make('directory'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         dump($directory->users); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     public function google5() | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         //List all users: https://developers.google.com/apps-script/advanced/admin-sdk-directory | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  | //        page = AdminDirectory.Users.list({ | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | //        domain: 'example.com', | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | //      orderBy: 'givenName', | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  | //      maxResults: 100, | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  | //      pageToken: pageToken | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  | //    }); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 108 |  | View Code Duplication |     public function google4() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         //Example list 100 users | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $directory = Google::make('directory'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         dump(get_class($directory->users)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         dump($directory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $r = $directory->users->listUsers([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 'domain' => 'iesebre.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 'maxResults' => 500 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             dump($r); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             dd($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 125 |  | View Code Duplication |     public function google7() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         //Example list 500 users. max results maxim value is 500 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $directory = Google::make('directory'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         dump(get_class($directory->users)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             $r = $directory->users->listUsers([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 'domain' => 'iesebre.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 'maxResults' => 500 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             dump($r); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             dd($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     public function google8() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         // Count total number of users | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $directory = Google::make('directory'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $count = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $pageToken = null; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 147 |  | View Code Duplication |         do { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 $r = $directory->users->listUsers([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                     'domain' => 'iesebre.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                     'maxResults' => 500, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                     'pageToken' => $pageToken | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 $pageToken = $r->nextPageToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | //                dump($pageToken); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | //                dump($r); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | //                dump($r->users); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 $count = $count + count($r->users); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                 dump('count: ' . $count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 dd($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } while ($pageToken); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         dd($count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     public function google10() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         return $this->service->localSync(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     public function esborrar() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         $googleClient = Google::getClient(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | //        dd($googleClient); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         $directory = Google::make('directory'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | //        dd($directory); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         $email = "[email protected]"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             $r = $directory->users->get($email); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             if($r) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                 echo "Name: ".$r->name->fullName."<br/>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                 echo "Suspended?: ".(($r->suspended === true) ? 'Yes' : 'No')."<br/>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |                 echo "Org/Unit/Path: ".$r->orgUnitPath."<br/>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |                 echo "User does not exist: $email<br/>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             dd('exception!!!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         // if the user doesn't exist, it's safe to create the new user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | //        $user_to_impersonate = '[email protected]'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | //        $scopes = array('https://www.googleapis.com/auth/admin.directory.user'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | //        $cred = new Google_Auth_AssertionCredentials( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | //            $service_account_name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | //            $scopes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | //            $key, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | //            'notasecret', // Default P12 password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | //            'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | //            $user_to_impersonate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | //        ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | //$dir = new Google_Service_Directory($client); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  | //$email = "[email protected]"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  | //$r = $dir->users->get($email); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  | //if($r) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  | //    echo "Name: ".$r->name->fullName."<br/>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  | //    echo "Suspended?: ".(($r->suspended === true) ? 'Yes' : 'No')."<br/>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  | //    echo "Org/Unit/Path: ".$r->orgUnitPath."<br/>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  | //} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  | //    echo "User does not exist: $email<br/>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  | //    // if the user doesn't exist, it's safe to create the new user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  | //} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 219 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 220 |  |  | } | 
            
                        
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.