Browse Source

User Control system, better statistic

Alexandr 5 years ago
parent
commit
d5a5e7195d

+ 1 - 0
.gitignore

@@ -10,3 +10,4 @@ Homestead.json
 Homestead.yaml
 npm-debug.log
 yarn-error.log
+/todo.txt

+ 8 - 0
app/Http/Controllers/Auth/LoginController.php

@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Auth;
 use App\Http\Controllers\Controller;
 use App\Providers\RouteServiceProvider;
 use Illuminate\Foundation\Auth\AuthenticatesUsers;
+use Illuminate\Http\Request;
 
 class LoginController extends Controller
 {
@@ -37,4 +38,11 @@ class LoginController extends Controller
     {
         $this->middleware('guest')->except('logout');
     }
+
+    // To control users status,
+    // overwrite original function on Illuminate\Foundation\AuthAuthenticatesUsers:
+    protected function credentials(Request $request)
+    {
+        return array_merge($request->only($this->username(), 'password'),['isEnabled'=>true]);
+    }
 }

+ 29 - 2
app/Http/Controllers/HomeController.php

@@ -2,11 +2,10 @@
 
 namespace App\Http\Controllers;
 
+use App\User;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\App;
 
-use \App\Music;
-
 class HomeController extends Controller
 {
     /**
@@ -28,4 +27,32 @@ class HomeController extends Controller
     {
         return view('home');
     }
+
+    public function usersControl(){
+        return view('users', ['user_list'=>User::all()]);
+    }
+
+    public function userSwitch(User $user){
+        $user->isEnabled ^= 1;
+        $user->save();
+        return redirect('users');
+    }
+
+    public function userShutdown(){
+        $users = User::all();
+        foreach ($users as $user) {
+            $user->isEnabled = false;
+            $user->save();
+        }
+        return redirect('users');
+    }
+
+    public function userGreenlight(){
+        $users = User::all();
+        foreach ($users as $user) {
+            $user->isEnabled = true;
+            $user->save();
+        }
+        return redirect('users');
+    }
 }

+ 5 - 1
app/Http/Controllers/MusicController.php

@@ -42,7 +42,11 @@ class MusicController extends Controller
 
         // Scan for files
         $fIndex = array();
-        $files = Storage::disk('local')->listContents('public/audio', true);
+        try {
+            $files = Storage::disk('local')->listContents('public/audio', true);
+        } catch (\Exception $e){
+            $files = array();
+        }
         //return $files;
 
         // Update storage index

+ 5 - 2
database/migrations/2014_10_12_000000_create_users_table.php

@@ -2,6 +2,7 @@
 
 use Illuminate\Database\Migrations\Migration;
 use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Hash;
 use Illuminate\Support\Facades\Schema;
 
 class CreateUsersTable extends Migration
@@ -16,7 +17,8 @@ class CreateUsersTable extends Migration
         Schema::create('users', function (Blueprint $table) {
             $table->id();
             $table->string('name');
-            $table->bigInteger('role')->default(0);
+            $table->boolean('isAdmin')->default(false);
+            $table->boolean('isEnabled')->default(true);
             $table->string('email')->unique();
             $table->timestamp('email_verified_at')->nullable();
             $table->string('password');
@@ -27,7 +29,8 @@ class CreateUsersTable extends Migration
         DB::table('users')->insert([
             'name' => 'admin',
             'email' => 'admin@example.com',
-            'password' => '12345678'
+            'password' => Hash::make('12345678'),
+            'isAdmin' => true
         ]);
     }
 

+ 3 - 3
public/js/myScript.js

@@ -3,7 +3,7 @@ $(document).ready(function () {
 
     // --------------------------------------
     // Async data loading
-     function asyncSend(tUri, sObj, isTextRes){
+     function asyncSend(tUri, sObj, isTxtResp){
          var res = -1;
          $.ajax({
              method: "GET",
@@ -11,7 +11,7 @@ $(document).ready(function () {
              data: {},
              dataType: "html",
          }).done(function (data) {
-             if(isTextRes) {
+             if(isTxtResp) {
                  sObj.innerText = data;
              } else {
                  console.log(data);
@@ -25,7 +25,7 @@ $(document).ready(function () {
          return res;
      }
 
-     // other buttons
+     // "Scan files" button
     $('#fScan').click(function () {
         $(this).addClass('disabled');
         asyncSend(scanRoute, null, false);

+ 11 - 5
resources/lang/en/ui.php

@@ -9,13 +9,19 @@ return[
     'pwd_lb2' => 'Confirm Password',
     'remember' => 'Remember Me',
     'forgot_pwd' => 'Forgot Your Password?',
-    'panel' => 'Listen',
+    'panel' => 'Listen Audios',
+    'panelC' => 'Local Users',
     'logout' => 'Logout',
-    'tb_col1' => 'Title',
-    'tb_col2' => 'Controls',
-    'tb_col3' => 'Play',
-    'tb_col4' => 'Downl.',
+    'tb_title' => 'Title',
+    'tb_ctrl' => 'Control',
+    'tb_play' => 'Play',
+    'tb_down' => 'Downl.',
+    'tb_uname' => 'User Name',
+    'back_catlog' => 'Back to music ctalog',
+    'disable_all' => 'Block all users',
+    'allow_all' => 'Unlock all users',
     'rescan' => 'Rescan for audio',
+    'umanage' => 'Users managment',
     'login_req' => 'Please login, to listen audio',
     'err_play' => 'Your browser does not support the audio element.',
     'docs' => 'Big catalog of classic orthodox songs, recorded in our cathedral',

+ 10 - 4
resources/lang/ru/ui.php

@@ -10,12 +10,18 @@ return[
     'remember' => 'Запомнить меня',
     'forgot_pwd' => 'Забали пароль?',
     'panel' => 'Наш архив',
+    'panelC' => 'пользователи',
     'logout' => 'Выйти',
-    'tb_col1' => 'Название',
-    'tb_col2' => 'Управление',
-    'tb_col3' => 'Слуш.',
-    'tb_col4' => 'Скач.',
+    'tb_title' => 'Название',
+    'tb_ctrl' => 'Управление',
+    'tb_play' => 'Слуш.',
+    'tb_down' => 'Скач.',
+    'tb_uname' => 'ФИО Пользователя',
+    'back_catlog' => 'Назад к катлогу',
+    'disable_all' => 'Заблокировать всех',
+    'allow_all' => 'Разаблокировать всех',
     'rescan' => 'Персканировать аудио',
+    'umanage' => 'Управление пользователями',
     'login_req' => 'Пожалуйста залогинстесь, чтобы слушать записи',
     'err_play' => 'Ваш браузер не поддерживает воспроизвдение аудио',
     'docs' => 'Большой каталог церковных песнопений, исполнненых знаменным распевом, записанны в Спасо-Андрониковом маонастыре',

+ 17 - 7
resources/views/home.blade.php

@@ -3,16 +3,24 @@
 @section('content')
     <div class="container">
         <div class="row justify-content-center">
-            <div class="col-md-9">
+            <div class="col-md-12">
                 <div class="card">
                     <div class="card-header d-flex flex-row">
                         <div
                             class="flex-grow-1 text-primary text-uppercase font-weight-bold"> {{ __('ui.panel') }}</div>
                         @auth
+                        @if(Auth::user()->isAdmin)
+                        <div class="btn-group">
                             <div class="btn btn-sm btn-outline-primary text-right" id="fScan">
                                 <i class="fas fa-retweet"></i>
                                 {{ __('ui.rescan') }}
                             </div>
+                            <a href="{{route('ucontrol')}}" class="btn btn-sm btn-outline-primary text-right">
+                                <i class="fas fa-user-cog"></i>
+                                {{ __('ui.umanage') }}
+                            </a>
+                        </div>
+                        @endif
                         @endauth
                     </div>
 
@@ -30,24 +38,26 @@
                             <table class="table table-bordered">
                                 <thead class="thead-dark">
                                 <tr>
-                                    <th scope="col">{{__('ui.tb_col1')}}</th>
-                                    <th scope="col" class="controls col-sm-2"><i class="fas fa-play-circle"></i> {{__('ui.tb_col3')}}</th>
-                                    <th scope="col" class="controls col-sm-2"><i class="fas fa-file-audio"></i> {{__('ui.tb_col4')}}</th>
+                                    <th scope="col">{{__('ui.tb_title')}}</th>
+                                    <th scope="col" class="controls col-sm-1"><i class="fas fa-play-circle"></i> {{__('ui.tb_play')}}</th>
+                                    <th scope="col" class="controls col-sm-1"><i class="fas fa-file-audio"></i> {{__('ui.tb_down')}}</th>
                                 </tr>
                                 </thead>
                                 <tbody>
                                 @foreach($song_list as $song)
                                     @if($song->type == 'dir')
                                         <tr class="thead-light">
-                                            <th colspan="3">{{$song->title}}</th>
+                                            <th colspan="4">{{$song->title}}</th>
                                         </tr>
                                         @elseif($song->type == 'txt')
                                         <tr class="thead-light">
-                                            <th colspan="3" class="font-weight-lighter">{{$song->description}}</th>
+                                            <th colspan="4" class="font-weight-lighter">{{$song->description}}</th>
                                         </tr>
                                     @else
                                     <tr>
-                                        <td style="text-align: left">{{$song->title}}</td>
+                                        <td style="text-align: left">
+                                            {{$song->title}}
+                                        </td>
                                         <td class="controls">
                                             <div class="btn-group">
                                                 <div class="btn btn-sm btn-outline-primary play_btn"

+ 78 - 0
resources/views/users.blade.php

@@ -0,0 +1,78 @@
+@extends('layouts.app')
+
+@section('content')
+    <div class="container">
+        <div class="row justify-content-center">
+            <div class="col-md-12">
+                <div class="card">
+                    <div class="card-header d-flex flex-row">
+                        <div class="flex-grow-1 text-primary text-uppercase font-weight-bold">
+                            {{ __('ui.panelC') }}
+                        </div>
+                            @auth
+                                @if(Auth::user()->isAdmin)
+                                    <div class="btn-group">
+                                        <a href="{{route('home')}}" class="btn btn-sm btn-outline-primary text-right">
+                                            <i class="fas fa-walking"></i>
+                                            {{ __('ui.back_catlog') }}
+                                        </a>
+                                        <a href="{{route('ushutdown')}}" class="btn btn-sm btn-outline-primary text-right">
+                                            <i class="fas fa-user-slash"></i>
+                                            {{ __('ui.disable_all') }}
+                                        </a>
+                                        <a href="{{route('ugreen')}}" class="btn btn-sm btn-outline-primary text-right">
+                                            <i class="fas fa-user"></i>
+                                            {{ __('ui.allow_all') }}
+                                        </a>
+                                    </div>
+                                @endif
+                            @endauth
+                    </div>
+
+                    <div class="card-body">
+                        @if (session('status'))
+                            <div class="alert alert-success" role="alert">
+                                {{ session('status') }}
+                            </div>
+                        @endif
+
+                        @guest
+                            <div class="alert alert-danger">{{ __('ui.login_req') }}</div>
+                        @else
+
+                            <table class="table table-bordered">
+                                <thead class="thead-dark">
+                                <tr>
+                                    <th scope="col">{{__('ui.tb_uname')}}</th>
+                                    <th scope="col" class="controls col-sm-1">{{__('ui.tb_ctrl')}}</th>
+                                </tr>
+                                </thead>
+                                <tbody>
+                                @foreach($user_list as $user)
+                                    @if(!$user->isAdmin)
+                                    <tr>
+                                        <td style="text-align: left; @if(!$user->isEnabled) text-decoration:line-through @endif">
+                                            {{$user->name}}
+                                        </td>
+                                        <td class="controls">
+                                            <div class="btn-group">
+                                                <a class="btn btn-sm btn-outline-primary play_btn"
+                                                     href="{{route('uswitch', $user->id)}}">
+                                                    <i class="fas @if(!$user->isEnabled)fa-user-slash @else fa-user @endif"></i>
+                                                </a>
+                                            </div>
+                                        </td>
+                                    </tr>
+                                    @endif
+                                @endforeach
+                                </tbody>
+                            </table>
+
+                        @endguest
+
+                    </div>
+                </div>
+            </div>
+        </div>
+    </div>
+@endsection

+ 4 - 0
routes/web.php

@@ -31,4 +31,8 @@ Route::get('/home', 'MusicController@index')->name('home');
 Route::get('/music/play/{music}/', 'MusicController@playlog')->name('mplay');
 Route::get('/music/download/{music}/', 'MusicController@dwlog')->name('mdown');
 Route::get('/music/scan/', 'MusicController@filerefresh')->name('rescan');
+Route::get('/users/', 'HomeController@usersControl')->name('ucontrol');
+Route::get('/users/{user}', 'HomeController@userSwitch')->name('uswitch');
+Route::get('/users_shutdown', 'HomeController@userShutdown')->name('ushutdown');
+Route::get('/users_allow', 'HomeController@userGreenlight')->name('ugreen');