Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\Http\Controllers\Controller;
- use App\Http\Requests\CityRequest;
- use App\State;
- use App\User;
- use Auth;
- use Carbon\Carbon;
- use DB;
- use Illuminate\Http\Request;
- class UserController extends Controller {
- /**
- * Create a new controller instance.
- *
- * @return void
- */
- public function __construct() {
- $this->middleware('auth');
- }
- public function finishRegister() {
- $states = State::all();
- return view('finishRegister', ['states' => $states]);
- }
- public function finishRegisterPost(CityRequest $request) {
- $putanjaDoResources = realpath(base_path('resources/views'));
- mkdir($putanjaDoResources . '\ ' . str_replace(".", "_", str_replace(" ", "_", $request['nameCompany'])));
- DB::table('cities')->insert(array(
- 'nameCity' => $request->input('nameCity'),
- 'ptt' => $request->input('ptt'),
- 'stateID' => $request->input('stateID'),
- ));
- $city = DB::table('cities')->orderBy('cityID', 'desc')->first();
- DB::table('companies')->insert(array(
- 'nameCompany' => $request->input('nameCompany'),
- 'cityID' => $city->cityID,
- 'numberUsers' => 1,
- 'numberLicenses' => 1,
- 'adress' => $request->input('adress'),
- 'phoneNumber' => $request->input('phoneNumber'),
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s'),
- ));
- $company = DB::table('companies')->orderBy('companyID', 'desc')->first();
- $id = Auth::user()->id;
- $user = User::find($id);
- $user->companyID = $company->companyID;
- $user->privilege = 'sa';
- $user->save();
- $KontrolerFirme = app_path() . "/Http/Controllers/" . str_replace('.', '_', str_replace(' ', '_', $company->nameCompany)) . "Controller.php";
- if (!file_exists($KontrolerFirme)) {
- $content = "<?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Auth;
- use DB;
- class " . str_replace('.', '_', str_replace(' ', '_', $company->nameCompany)) . "Controller extends Controller
- {
- //Kraj funkcija
- }";
- //ubacivanje kontrolera u file "NazivFirmeController"
- $fp = fopen(app_path() . "/Http/Controllers/" . str_replace('.', '_', str_replace(' ', '_', $company->nameCompany)) . "Controller.php", "wb");
- fwrite($fp, $content);
- fclose($fp);
- }
- $RuteFirme = app_path() . "/Http/Routes/" . str_replace('.', '_', str_replace(' ', '_', $company->nameCompany)) . ".php";
- if (!file_exists($RuteFirme)) {
- $fp = fopen(app_path() . "/Http/Routes/" . str_replace('.', '_', str_replace(' ', '_', $company->nameCompany)) . ".php", "wb");
- fwrite($fp, "<?php \n ");
- fclose($fp);
- }
- $routesPutanja = app_path() . "/Http/routes.php";
- $content = "require app_path('Http/Routes/" . str_replace('.', '_', str_replace(' ', '_', $company->nameCompany)) . ".php');\n //Kraj ruta";
- $search = "//Kraj ruta";
- $replace = "\n" . $content;
- file_put_contents($routesPutanja, str_replace($search, $replace, file_get_contents($routesPutanja)));
- return view('activities');
- }
- public function getUsers() {
- $msg = NULL;
- $id = Auth::user()->companyID;
- $users = User::all()->where('companyID', $id);
- return view('users.users')->with('users', $users);
- }
- public function addUser() {
- $companyid = Auth::user()->companyID;
- $company = DB::table('companies')->where('companyID', $companyid)->first();
- if ($company->numberUsers == $company->numberLicenses) {
- $msg = "You need to buy more licences!";
- return redirect()->action('UserController@getUsers')->with('msg', $msg);
- }
- return view('users.addUser');
- }
- public function infoUser($id) {
- $user = DB::table('users')
- ->join('companies', 'users.companyID', '=', 'companies.companyID')
- ->where('id', $id)
- ->first();
- return view('users.infoUser')->with('user', $user);
- }
- public function editUser($id) {
- $user = DB::table('users')->where('id', $id)
- ->first();
- return view('users.editUser')->with('user', $user);
- }
- public function deleteUser($id) {
- $userid = Auth::user()->id;
- $LogedUser = User::find($userid);
- $companyID = Auth::user()->companyID;
- $user = DB::table('users')->where('id', $id)->first();
- if ($user->privilege !== 'SA') {
- User::destroy($id);
- DB::table('log')->insert(array(
- 'companyID' => $companyID,
- 'UserName' => $LogedUser->name,
- 'Action' => 'DU',
- 'Description' => 'USER THAT HAS BEEN DELETED' . $user->name,
- 'created_at' => Carbon::now(),
- 'updated_at' => Carbon::now(),
- ));
- $msg = "You have secessfully deleted user!";
- } else {
- $msg = "You are not allowed to do this action!";
- }
- return redirect()->action('UserController@getUsers')->with('msg', $msg);
- }
- public function updateUser($id, Request $request) {
- $userid = Auth::user()->id;
- $user = User::find($userid);
- $companyID = Auth::user()->companyID;
- $userForEdit = DB::table('users')->where('id', $id)->first();
- $password = $request['password'];
- $password_confirmation = $request['password_confirmation'];
- if ($password !== $password_confirmation) {
- $msg = "Password do not match";
- return redirect()->action('UserController@getUsers')->with('msg', $msg);
- }
- $name = $request['name'];
- $email = $request['email'];
- $password = bcrypt($request['password']);
- $privilege = $request['privilege'];
- DB::table('users')->where('id', $id)->update(array(
- 'name' => $name,
- 'email' => $email,
- 'password' => $password,
- 'privilege' => $privilege,
- ));
- DB::table('log')->insert(array(
- 'companyID' => $companyID,
- 'UserName' => $user->name,
- 'Action' => 'EU',
- 'Description' => 'USER THAT HAS BEEN EDITED ' . $userForEdit->name,
- 'created_at' => Carbon::now(),
- 'updated_at' => Carbon::now(),
- ));
- $msg = "You have secessfully edited user!";
- return redirect()->action('UserController@getUsers')->with('msg', $msg);
- }
- public function addUserPost(Request $request) {
- $userid = Auth::user()->id;
- $user = User::find($userid);
- $companyID = Auth::user()->companyID;
- $password = $request['password'];
- $password_confirmation = $request['password_confirmation'];
- if ($password !== $password_confirmation) {
- $msg = "Password do not match";
- return redirect()->action('UserController@getUsers')->with('msg', $msg);
- }
- $name = $request['name'];
- $email = $request['email'];
- $password = bcrypt($request['password']);
- $privilege = $request['privilege'];
- $companyID = Auth::user()->companyID;
- DB::table('users')->insert(array(
- 'name' => $name,
- 'email' => $email,
- 'password' => $password,
- 'privilege' => $privilege,
- 'companyID' => $companyID,
- ));
- DB::table('log')->insert(array(
- 'companyID' => $companyID,
- 'UserName' => $user->name,
- 'Action' => 'AU',
- 'Description' => 'USER THAT HAS BEEN ADDED' . $name,
- 'created_at' => Carbon::now(),
- 'updated_at' => Carbon::now(),
- ));
- $msg = "You have secessfully added a user!";
- return redirect()->action('UserController@getUsers')->with('msg', $msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement