/** * This function will create a new user object and return the newly created user object. * * @param array $userInfo This should have the properties: username, firstname, lastname, password, ui_language * * @return mixed */ public function registerUser(array $userInfo, $userLanguage) { $user = \User::create($userInfo); //make the first user an admin if (\User::all()->count() <= 1) { $user->is_admin = 1; } // Trim trailing whitespace from user first and last name. $user->firstname = trim($user->firstname); $user->lastname = trim($user->lastname); $user->save(); \Setting::create(['ui_language' => $userLanguage, 'user_id' => $user->id]); /* Add welcome note to user - create notebook, tag and note */ //$notebookCreate = Notebook::create(array('title' => Lang::get('notebooks.welcome_notebook_title'))); $notebookCreate = new \Notebook(); $notebookCreate->title = Lang::get('notebooks.welcome_notebook_title'); $notebookCreate->save(); $notebookCreate->users()->attach($user->id, ['umask' => \PaperworkHelpers::UMASK_OWNER]); //$tagCreate = Tag::create(array('title' => Lang::get('notebooks.welcome_note_tag'), 'visibility' => 0)); $tagCreate = new \Tag(); $tagCreate->title = Lang::get('notebooks.welcome_note_tag'); $tagCreate->visibility = 0; $tagCreate->user_id = $user->id; $tagCreate->save(); //$tagCreate->users()->attach($user->id); $noteCreate = new \Note(); $versionCreate = new \Version(['title' => Lang::get('notebooks.welcome_note_title'), 'content' => Lang::get('notebooks.welcome_note_content'), 'content_preview' => mb_substr(strip_tags(Lang::get('notebooks.welcome_note_content')), 0, 255), 'user_id' => $user->id]); $versionCreate->save(); $noteCreate->version()->associate($versionCreate); $noteCreate->notebook_id = $notebookCreate->id; $noteCreate->save(); $noteCreate->users()->attach($user->id, ['umask' => \PaperworkHelpers::UMASK_OWNER]); $noteCreate->tags()->sync([$tagCreate->id]); return $user; }
public function checkSandstormUsers() { if (Config::get('paperwork.emergency_export') && DB::table('migrations')->where('batch', '=', 1)->count() == Config::get('paperwork.emergency_export_count')) { $credentials = ["username" => "sandstorm_dummy", "password" => "sandstorm_dummy"]; if (Auth::attempt($credentials)) { $settings = Setting::where('user_id', '=', Auth::user()->id)->first(); Session::put('ui_language', $settings->ui_language); return View::make('user.emergency_export'); } } else { // get permission via HTTP_X_SANDSTORM header $sandstorm_permissions = $_SERVER['HTTP_X_SANDSTORM_PERMISSIONS']; // Only when we are admin, we check and create users if ($sandstorm_permissions == "admin,write,read") { // check for admin user if (User::where('username', '=', 'sandstorm_admin')->count() == 0) { $sandstorm_admin = User::create(Input::except('_token', 'password_confirmation', 'ui_language')); if ($sandstorm_admin) { //make the first user an admin $sandstorm_admin->firstname = "sandstorm_admin"; $sandstorm_admin->lastname = " "; $sandstorm_admin->username = "******"; $sandstorm_admin->password = "******"; $sandstorm_admin->is_admin = 1; $sandstorm_admin->save(); $setting_sandstorm_admin = Setting::create(['ui_language' => 'en', 'user_id' => $sandstorm_admin->id]); } } else { $sandstorm_admin = User::where('username', '=', 'sandstorm_admin'); } // Then the read & write user if (User::where('username', '=', 'sandstorm_readwrite')->count() == 0) { $sandstorm_readwrite = User::create(Input::except('_token', 'password_confirmation', 'ui_language')); if ($sandstorm_readwrite) { $sandstorm_readwrite->firstname = "sandstorm_readwrite"; $sandstorm_readwrite->lastname = " "; $sandstorm_readwrite->username = "******"; $sandstorm_readwrite->password = "******"; $sandstorm_readwrite->save(); $setting_sandstorm_readwrite = Setting::create(['ui_language' => 'en', 'user_id' => $sandstorm_readwrite->id]); } } else { $sandstorm_readwrite = User::where('username', '=', 'sandstorm_readwrite'); } // Then the read only user if (User::where('username', '=', 'sandstorm_readonly')->count() == 0) { $sandstorm_readonly = User::create(Input::except('_token', 'password_confirmation', 'ui_language')); if ($sandstorm_readonly) { $sandstorm_readonly->firstname = "sandstorm_readonly"; $sandstorm_readonly->lastname = " "; $sandstorm_readonly->username = "******"; $sandstorm_readonly->password = "******"; $sandstorm_readonly->save(); $setting_sandstorm_readonly = Setting::create(['ui_language' => 'en', 'user_id' => $sandstorm_readonly->id]); } } else { $sandstorm_readonly = User::where('username', '=', 'sandstorm_readonly'); } } // Now that the required users are there we create the default if (Notebook::all()->count() == 0 && Tag::all()->count() == 0 && Note::all()->count() == 0) { // Notebook ... $notebookCreate = new Notebook(); $notebookCreate->title = Lang::get('notebooks.welcome_notebook_title'); $notebookCreate->save(); $notebookCreate->users()->attach($sandstorm_readonly->id, ['umask' => PaperworkHelpers::UMASK_READONLY]); $notebookCreate->users()->attach($sandstorm_readwrite->id, ['umask' => PaperworkHelpers::UMASK_READWRITE]); $notebookCreate->users()->attach($sandstorm_admin->id, ['umask' => PaperworkHelpers::UMASK_OWNER]); // Tag ... $tagCreate = new Tag(); $tagCreate->title = Lang::get('notebooks.welcome_note_tag'); $tagCreate->visibility = 1; $tagCreate->user_id = $sandstorm_admin->id; $tagCreate->save(); // Note ... $noteCreate = new Note(); $versionCreate = new Version(['title' => Lang::get('notebooks.welcome_note_title'), 'content' => Lang::get('notebooks.welcome_note_content'), 'content_preview' => mb_substr(strip_tags(Lang::get('notebooks.welcome_note_content')), 0, 255), 'user_id' => $sandstorm_admin->id]); $versionCreate->save(); $noteCreate->version()->associate($versionCreate); $noteCreate->notebook_id = $notebookCreate->id; $noteCreate->save(); $noteCreate->users()->attach($sandstorm_readonly->id, ['umask' => PaperworkHelpers::UMASK_READONLY]); $noteCreate->users()->attach($sandstorm_readwrite->id, ['umask' => PaperworkHelpers::UMASK_READWRITE]); $noteCreate->users()->attach($sandstorm_admin->id, ['umask' => PaperworkHelpers::UMASK_OWNER]); $noteCreate->tags()->sync([$tagCreate->id]); } // login if ($sandstorm_permissions == "read") { $credentials = ["username" => "sandstorm_readonly", "password" => "sandstorm_readonly"]; } if ($sandstorm_permissions == "write,read") { $credentials = ["username" => "sandstorm_readwrite", "password" => "sandstorm_readwrite"]; } if ($sandstorm_permissions == "admin,write,read") { $credentials = ["username" => "sandstorm_admin", "password" => "sandstorm_admin"]; } if (Auth::attempt($credentials)) { $settings = Setting::where('user_id', '=', Auth::user()->id)->first(); Session::put('ui_language', $settings->ui_language); return Redirect::route("/"); } } }