FEATURE #1359: registration works now

Some basic form of registration is done. It does not look very nice, however I was able to create new user. This part will need a lots of other improvements, however new user was created. Yay!
This commit is contained in:
Michal Kváček
2016-01-21 01:08:03 +01:00
parent 559289dd39
commit 2f7df75b83
9 changed files with 257 additions and 53 deletions
+64 -4
View File
@@ -2,12 +2,72 @@
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Models\User;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class UserManagement extends Controller
{
//
public function getNewUser()
{
// define list of roles, this list will be used in template for selecting proper role for user
$roles = [
User::ROLE_ADMIN => 'admin/userManagement.role_' . User::ROLE_ADMIN,
User::ROLE_EDITOR => 'admin/userManagement.role_' . User::ROLE_EDITOR,
User::ROLE_VISITOR => 'admin/userManagement.role_' . User::ROLE_VISITOR
];
// render view
return $this->view('admin.userManagement.new', ['roles' => $roles]);
}
public function getEditExistingUser($id)
{
// try to find user
$user = User::findOrFail($id);
// render view
return $this->view('admin.userManagement.edit', ['user' => $user]);
}
public function getOverview($state = null)
{
echo $state;
}
/**
* Create new user
*
* This method saves new user into database
*
* @param Request $request
*/
protected function postRegistration(Request $request)
{
// validate input data
$this->validate($request, [
'role' => 'required',
'email' => 'required|email|max:128|unique:users',
'password' => 'required|min:6'
]);
// when user did not provide any username, use his email
$username = isset($request->username) && !empty($request->username) ? $request->username : $request->email;
// create new user
$user = new User();
$user->email = $request->email;
$user->username = $username;
$user->role = $request->role;
$user->password = Hash::make($request->role);
// store user into database
$user->save();
// @todo add some redirect, inform user etc.
}
}
+17
View File
@@ -10,4 +10,21 @@ use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
/**
* Custom wrapper for generating view.
*
* This method will be used for switching among templates.
*
* @todo add template support
*
* @param null $view
* @param array $data
* @param array $mergeData
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
protected function view($view = null, $data = [], $mergeData = []) {
return view($view, $data, $mergeData);
}
}
+32
View File
@@ -23,11 +23,43 @@
*/
Route::group(['middleware' => ['web']], function () {
// display login form
Route::get('login', function () {
return view('admin.loginForm.form');
});
// ---------------------------------------------------------------
// @todo move to admin group (protect it)
// display form for creating new user
Route::get('admin/users/new', 'Admin\UserManagement@getNewUser')
->name('users.new');
// display form for edit of existing user
Route::get('admin/users/edit/{id}', 'Admin\UserManagement@getEditExistingUser')
->name('users.edit');
// display user detail in admin
Route::get('admin/users/{id}', 'Admin\UserManagement@getDetail')
->name('users.detail');
// display list of all users registered into our system
Route::get('admin/users/overview/{state?}', 'Admin\UsersManagement@getOverview')
->name('users.overview');
// POST methods
// ------------------------------------------
// create new user
Route::post('admin/users/new', 'Admin\UserManagement@postRegistration')
->name('admin.users.registration');
// edit existing user
Route::post('admin/users/new', 'Admin\UserManagement@postRegistration')
->name('admin.users.edit');
// handle login
Route::post('admin/login', 'Admin\Login@postLogin')
->name('login');
});
+28 -6
View File
@@ -2,34 +2,56 @@
namespace App\Models;
use App\Http\Requests\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Symfony\Component\Process\Exception\InvalidArgumentException;
/**
* Class User
*/
class User extends Model
{
public $timestamps = false;
protected $fillable = [
'email',
'username',
'password',
'remember_token',
'role',
'name',
'status'
];
protected $guarded = [];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
'password',
'remember_token',
];
const STATE_DELETED = 'deleted';
const STATE_BLOCKED = 'blocked';
const STATE_ACTIVE = 'active';
const ROLE_VISITOR = 'visitor';
const ROLE_EDITOR = 'editor';
const ROLE_ADMIN = 'admin';
/**
* Validation method for input data
*
* @param array $data
*
* @return
*/
public static function validate(array $data)
{
$valid = true;
if (!$valid) {
throw new ValidationException(Validator::class, 'Invalid request');
}
}
}
+40 -42
View File
@@ -1,5 +1,5 @@
-- MySQL Script generated by MySQL Workbench
-- St 20. leden 2016, 19:50:50 CET
-- Čt 21. leden 2016, 00:19:53 CET
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering
@@ -12,19 +12,17 @@ SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema minicms
-- Table `users`
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Table `user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `user` (
CREATE TABLE IF NOT EXISTS `users` (
`id` INT NOT NULL AUTO_INCREMENT,
`email` VARCHAR(64) NOT NULL COMMENT 'E-mail is used as a contact and also as a username',
`username` VARCHAR(64) NOT NULL COMMENT 'Username can be used also for login. Default value is users email.',
`password` VARCHAR(128) NOT NULL COMMENT 'Password hash',
`role` ENUM('visitor', 'editor', 'admin') NOT NULL DEFAULT 'visitor' COMMENT 'User\'s role determines his permissions in the system.',
`remember_token` VARCHAR(128) NULL DEFAULT NULL COMMENT 'Token used for \"remember me\" feature',
`name` VARCHAR(128) NULL DEFAULT NULL,
`status` ENUM('deleted', 'blocked', 'active') NOT NULL DEFAULT 'active' ,
`status` ENUM('deleted', 'blocked', 'active') NOT NULL DEFAULT 'active',
PRIMARY KEY (`id`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC))
ENGINE = InnoDB
@@ -32,9 +30,9 @@ COMMENT = 'Table with users who are allowed to log into system';
-- -----------------------------------------------------
-- Table `module`
-- Table `modules`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `module` (
CREATE TABLE IF NOT EXISTS `modules` (
`handler` VARCHAR(64) NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`name` VARCHAR(64) NOT NULL,
@@ -44,9 +42,9 @@ ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `file`
-- Table `files`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `file` (
CREATE TABLE IF NOT EXISTS `files` (
`id` INT NOT NULL,
`user_id` INT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -57,7 +55,7 @@ CREATE TABLE IF NOT EXISTS `file` (
INDEX `fk_file_user1_idx` (`user_id` ASC),
CONSTRAINT `fk_file_user1`
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
@@ -65,9 +63,9 @@ COMMENT = 'Table containing files which were uploaded into system.';
-- -----------------------------------------------------
-- Table `content`
-- Table `contents`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `content` (
CREATE TABLE IF NOT EXISTS `contents` (
`id` INT NOT NULL,
`user_id` INT NOT NULL,
`module_handler` VARCHAR(64) NOT NULL,
@@ -92,17 +90,17 @@ CREATE TABLE IF NOT EXISTS `content` (
INDEX `fk_content_file1_idx` (`title_photo` ASC),
CONSTRAINT `fk_content_user1`
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_content_module1`
FOREIGN KEY (`module_handler`)
REFERENCES `module` (`handler`)
REFERENCES `modules` (`handler`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_content_file1`
FOREIGN KEY (`title_photo`)
REFERENCES `file` (`id`)
REFERENCES `files` (`id`)
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
@@ -128,12 +126,12 @@ CREATE TABLE IF NOT EXISTS `content_history` (
INDEX `fk_content_history_content1_idx` (`content_id` ASC),
CONSTRAINT `fk_content_history_users`
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_content_history_content1`
FOREIGN KEY (`content_id`)
REFERENCES `content` (`id`)
REFERENCES `contents` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
@@ -141,9 +139,9 @@ COMMENT = 'Table for holding history of changes on some content.';
-- -----------------------------------------------------
-- Table `widget_type`
-- Table `widget_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `widget_type` (
CREATE TABLE IF NOT EXISTS `widget_types` (
`handler` VARCHAR(64) NOT NULL COMMENT 'Class wich handles everything about this widget type',
`name` VARCHAR(64) NOT NULL COMMENT 'Name is used only in admin for better readability.',
`description` TEXT NULL DEFAULT NULL COMMENT 'This field also improves readability.',
@@ -153,9 +151,9 @@ COMMENT = 'Widget type table contains information about all installed widgets. T
-- -----------------------------------------------------
-- Table `widget`
-- Table `widgets`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `widget` (
CREATE TABLE IF NOT EXISTS `widgets` (
`id` INT NOT NULL,
`widget_type_handler` VARCHAR(64) NOT NULL,
`user_id` INT NOT NULL COMMENT 'User ID who created this widget',
@@ -168,12 +166,12 @@ CREATE TABLE IF NOT EXISTS `widget` (
INDEX `fk_widget_widget_type1_idx` (`widget_type_handler` ASC),
CONSTRAINT `fk_widget_user1`
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_widget_widget_type1`
FOREIGN KEY (`widget_type_handler`)
REFERENCES `widget_type` (`handler`)
REFERENCES `widget_types` (`handler`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
@@ -181,9 +179,9 @@ COMMENT = 'Widget is a small piece of content, which can be inserted into widget
-- -----------------------------------------------------
-- Table `widget_area`
-- Table `widget_areas`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `widget_area` (
CREATE TABLE IF NOT EXISTS `widget_areas` (
`id` INT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
@@ -209,21 +207,21 @@ CREATE TABLE IF NOT EXISTS `widget_in_widget_area` (
INDEX `position` (`position` ASC),
CONSTRAINT `fk_widget_has_widget_area_widget1`
FOREIGN KEY (`widget_id`)
REFERENCES `widget` (`id`)
REFERENCES `widgets` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_widget_has_widget_area_widget_area1`
FOREIGN KEY (`widget_area_id`)
REFERENCES `widget_area` (`id`)
REFERENCES `widget_areas` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `custom_field`
-- Table `custom_fields`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `custom_field` (
CREATE TABLE IF NOT EXISTS `custom_fields` (
`id` INT NOT NULL,
`handler` VARCHAR(64) NOT NULL COMMENT 'Class name, which handles saving and retrieving given custom field.',
`name` VARCHAR(128) NULL,
@@ -250,21 +248,21 @@ CREATE TABLE IF NOT EXISTS `user_has_custom_field` (
INDEX `fk_user_has_custom_field_user1_idx` (`user_id` ASC),
CONSTRAINT `fk_user_has_custom_field_user1`
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
REFERENCES `users` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_user_has_custom_field_custom_field1`
FOREIGN KEY (`custom_field_id`)
REFERENCES `custom_field` (`id`)
REFERENCES `custom_fields` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `custom_field_type`
-- Table `custom_field_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `custom_field_type` (
CREATE TABLE IF NOT EXISTS `custom_field_types` (
`id` INT NOT NULL,
`name` VARCHAR(45) NOT NULL,
`custom_field_id` INT NOT NULL,
@@ -273,7 +271,7 @@ CREATE TABLE IF NOT EXISTS `custom_field_type` (
INDEX `fk_custom_field_type_custom_field1_idx` (`custom_field_id` ASC),
CONSTRAINT `fk_custom_field_type_custom_field1`
FOREIGN KEY (`custom_field_id`)
REFERENCES `custom_field` (`id`)
REFERENCES `custom_fields` (`id`)
ON DELETE RESTRICT
ON UPDATE NO ACTION)
ENGINE = InnoDB
@@ -292,21 +290,21 @@ CREATE TABLE IF NOT EXISTS `module_has_custom_field` (
INDEX `fk_module_has_custom_field_module1_idx` (`module_handler` ASC),
CONSTRAINT `fk_module_has_custom_field_module1`
FOREIGN KEY (`module_handler`)
REFERENCES `module` (`handler`)
REFERENCES `modules` (`handler`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_module_has_custom_field_custom_field1`
FOREIGN KEY (`custom_field_id`)
REFERENCES `custom_field` (`id`)
REFERENCES `custom_fields` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `action_log_type`
-- Table `action_log_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `action_log_type` (
CREATE TABLE IF NOT EXISTS `action_log_types` (
`id` INT NOT NULL,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`id`))
@@ -328,12 +326,12 @@ CREATE TABLE IF NOT EXISTS `action_log` (
INDEX `fk_action_log_user1_idx` (`user_id` ASC),
CONSTRAINT `fk_action_log_action_log_type1`
FOREIGN KEY (`type_id`)
REFERENCES `action_log_type` (`id`)
REFERENCES `action_log_types` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_action_log_user1`
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
@@ -0,0 +1,13 @@
<?php
return [
'name' => 'Full name',
'email' => 'Email',
'password' => 'Password',
'roles' => 'Please, select user\'s role',
'role_admin' => 'Admin',
'role_editor' => 'Editor',
'role_visitor' => 'Visitor',
'register' => 'Create user'
];
@@ -0,0 +1 @@
edituji uzivatele {{$id}}.
@@ -0,0 +1,61 @@
@extends('admin.layout')
@section('content')
<form method="POST" action="{{route('users.new')}}">
{!! csrf_field() !!}
<label>
@lang('admin/userManagement.name')
<input type="text" placeholder="@lang('admin/userManagement.name')" name="name">
</label>
<label>
@lang('admin/userManagement.email')
<input type="text" name="email" placeholder="somebody@example.com">
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</label>
<label>
@lang('admin/userManagement.password')
<input type="text" name="password">
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</label>
<div>
@lang('admin/userManagement.roles')
@foreach($roles as $role => $translation)
<label>
@lang($translation)
<input type="radio" name="role" value="{{$role}}">
</label>
@endforeach
@if ($errors->has('role'))
<span class="help-block">
<strong>{{ $errors->first('role') }}</strong>
</span>
@endif
</div>
<label>
<input type="submit" value="@lang('admin/userManagement.register')">
</label>
</form>
@stop