diff --git a/application/controllers/Login.php b/application/controllers/Login.php index a3d257f..f7445f6 100644 --- a/application/controllers/Login.php +++ b/application/controllers/Login.php @@ -13,7 +13,7 @@ class Login extends MY_Controller { { parent::__construct(); - $this->load->library( 'beheer/CreateUserSession' ); + $this->load->library( 'beheer/session/CreateUserSession' ); } public function index() diff --git a/application/controllers/beheer/usersController.php b/application/controllers/beheer/usersController.php index 706b702..b5866f9 100644 --- a/application/controllers/beheer/usersController.php +++ b/application/controllers/beheer/usersController.php @@ -30,4 +30,27 @@ class usersController extends MY_Controller { $this->json( $users ); } + + public function saveUser() + { + $userValidation = new UserValidation(); + + if ( ! $userValidation->getValidationResult() ) + return $this->json( [ + 'success'=> false, 'messages' =>$userValidation->getErrorMessages()] ); + + $user = $this->input->post(); + + $userID = $user['id']; + unset($user['id']); + + if( $userID == 0 ) + { + createNewUser(); + } + + $userID = User::saveUser( $userID, $user); + + $this->json( ['success' => TRUE] ); + } } \ No newline at end of file diff --git a/application/core/MY_Controller.php b/application/core/MY_Controller.php index 11a0b8f..87dd1eb 100644 --- a/application/core/MY_Controller.php +++ b/application/core/MY_Controller.php @@ -15,7 +15,7 @@ class MY_Controller extends CI_Controller { { parent::__construct(); - $this->load->library( 'beheer/RetrieveUserSession' ); + $this->load->library( 'beheer/session/RetrieveUserSession' ); $this->handle(); } diff --git a/application/libraries/beheer/CreateUserSession.php b/application/libraries/beheer/session/CreateUserSession.php similarity index 100% rename from application/libraries/beheer/CreateUserSession.php rename to application/libraries/beheer/session/CreateUserSession.php diff --git a/application/libraries/beheer/RetrieveUserSession.php b/application/libraries/beheer/session/RetrieveUserSession.php similarity index 100% rename from application/libraries/beheer/RetrieveUserSession.php rename to application/libraries/beheer/session/RetrieveUserSession.php diff --git a/application/libraries/beheer/user/CreateUser.php b/application/libraries/beheer/user/CreateUser.php new file mode 100644 index 0000000..f215e4d --- /dev/null +++ b/application/libraries/beheer/user/CreateUser.php @@ -0,0 +1,16 @@ +handle(); + } + + public function setNakedPassword( $nakedPassword ) + { + $this->nakedPassword = $nakedPassword; + } + + public function setSalt( $salt ) + { + $this->salt = $salt; + } + + private function handle() + { + $this->checkNakedPassword(); + + $this->checkSalt(); + + $this->setSaltedPassword(); + } + + private function checkNakedPassword() + { + if( ! $this->nakedPassword ) $this->createNakedPassword(); + + if( strlen($this->nakedPassword) < 6 ) return; + } + + private function checkSalt() + { + if( ! $this->salt ) $this->createSalt(); + } + + private function createNakedPassword() + { + $pass = $this->getRandomString(8); + + $this->nakedPassword = $pass; + } + + private function createSalt() + { + $salt = $this->getRandomString(8); + + $this->salt = $salt; + } + + private function setSaltedPassword() + { + $this->saltedPassword = sha1($this->) + } + + private function getRandomString( $length=8 ) + { + $rand = ''; + $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; + $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache + for ($i = 0; $i < $length; $i++) { + $n = rand(0, $alphaLength); + $rand.= $alphabet[$n]; + } + } +} \ No newline at end of file diff --git a/application/libraries/validation/Validation.php b/application/libraries/validation/Validation.php new file mode 100644 index 0000000..5aa2c6b --- /dev/null +++ b/application/libraries/validation/Validation.php @@ -0,0 +1,190 @@ +ci =& get_instance(); + } + + public function validate( $fieldValue, $validationField = FALSE ) + { + $this->setValidationField( $validationField ); + + $this->setFieldValue( $fieldValue ); + + $this->handle(); + } + + public function setValidationField( $validationfield ) + { + $this->validationField = $validationfield; + } + + public function getErrorMessages() + { + return $this->errorMessages; + } + + public function getValidationResult() + { + return $this->validationResult; + } + + private function handle() + { + $this->setValidations(); + + $this->checkValidations(); + + $this->setValidationResult(); + } + + private function setValidations() + { + $this->validations = explode( '|', $this->validationField['validations'] ); + } + + private function setFieldValue( $fieldValue ) + { + $this->fieldValue = $fieldValue; + } + + private function setValidationResult() + { + if ( count( $this->errorMessages ) == 0 ) return $this->validationResult = TRUE; + + $this->validationResult = FALSE; + } + + private function checkValidations() + { + foreach ( $this->validations as $this->validation ) + { + if ( $this->validation == 'required' ) $this->checkRequired(); + if ( $this->validation == 'alpha' ) $this->checkAlfa(); + if ( $this->validation == 'alphaNumeric' ) $this->checkAlfanumeric(); + if ( $this->validation == 'alphaNumericSpaces' ) $this->checkAlphaNumericSpaces(); + if ( $this->validation == 'alphaDash' ) $this->checkAlphaDash(); + if ( $this->validation == 'numeric' ) $this->checkNumeric(); + if ( $this->validation == 'integer' ) $this->checkInteger(); + if ( $this->validation == 'decimal' ) $this->checkDecimal(); + if ( $this->validation == 'email' ) $this->checkEmail(); + if ( $this->validation == 'unique' ) $this->checkUnique(); + if ( substr( $this->validation, 0, 9 ) == 'minlength' ) $this->checkMinlength(); + if ( substr( $this->validation, 0, 9 ) == 'maxlength' ) $this->checkMaxlength(); + } + } + + public function checkRequired() + { + if ( ! $this->fieldValue || empty( $this->fieldValue ) || strlen( $this->fieldValue ) == 0 ) + $this->errorMessages[] = 'veld is verplicht'; + } + + public function checkAlfa() + { + $value = ctype_alpha( $this->fieldValue ); + + if ( ! $value ) $this->errorMessages[] = 'alleen letters toegestaan'; + } + + public function checkAlfaNumeric() + { + $value = ctype_alnum( (string) $this->fieldValue ); + + if ( ! $value ) $this->errorMessages[] = 'alleen letters en cijfers toegestaan'; + + return $value; + } + + public function checkAlphaNumericSpaces() + { + $value = (bool) preg_match( '/^[A-Z0-9 ]+$/i', $this->fieldValue ); + + if ( ! $value ) $this->errorMessages[] = 'alleen letters, cijfers en spaties toegestaan'; + } + + public function checkAlphaDash() + { + $value = (bool) preg_match( '/^[a-z0-9_-]+$/i', $this->fieldValue ); + + if ( ! $value ) $this->errorMessages[] = 'alleen letters, cijfers en streepjes toegestaan'; + } + + public function checkNumeric() + { + $value = (bool) preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $this->fieldValue ); + + if ( ! $value ) $this->errorMessages[] = 'alleen getallen toegestaan'; + } + + public function checkInteger() + { + $value = (bool) preg_match( '/^[\-+]?[0-9]+$/', $this->fieldValue ); + + if ( ! $value ) $this->errorMessages[] = 'alleen gehele getallen toegestaan'; + } + + public function checkDecimal() + { + $value = (bool) preg_match( '/^[\-+]?[0-9]+\.[0-9]+$/', $this->fieldValue ); + + if ( ! $value ) $this->errorMessages[] = 'alleen getallen toegestaan'; + } + + public function checkEmail() + { + if ( ! $this->fieldValue || empty( $this->fieldValue ) || strlen( $this->fieldValue ) == 0 ) return; + + $value = (bool) filter_var( $this->fieldValue, FILTER_VALIDATE_EMAIL ); + + if ( ! $value ) $this->errorMessages[] = 'ongeldig emailadres'; + } + + public function checkUnique() + { + $recordID = $this->ci->input->post( 'id' ); + + $model = $this->validationField['model']; + $field = $this->validationField['field']; + + $value = $this->ci + ->$model + ->checkUnique( $recordID, $field, $this->fieldValue ); + + if ( ! $value ) $this->errorMessages[] = 'invoer moet uniek zijn'; + } + + private function checkMinlength() + { + $length = explode( '-', $this->validation ); + + if ( strlen( $this->fieldValue ) < $length[1] ) + $this->errorMessages[] = 'moet minimaal uit ' . $length[1] . ' karakters bestaan'; + } + + private function checkMaxlength() + { + $length = explode( $this->validation, '-' ); + + if ( strlen( $this->fieldValue ) > $length[1] ) + $this->errorMessages[] = 'mag maximaal uit ' . $length[1] . ' karakters bestaan'; + } + + +} \ No newline at end of file diff --git a/application/libraries/validation/inputValidations/BaseValidation.php b/application/libraries/validation/inputValidations/BaseValidation.php new file mode 100644 index 0000000..0b8cba2 --- /dev/null +++ b/application/libraries/validation/inputValidations/BaseValidation.php @@ -0,0 +1,91 @@ +ci =& get_instance(); + + $this->validateInput(); + } + + public function getValidationResult() + { + return $this->validationResult; + } + + public function validateInput() + { + $this->loopFields(); + } + + public function getErrorMessages( ) + { + return $this->errorMessages; + } + + private function loopFields() + { + foreach ( $this->validationFields as $this->key => $this->validationField ) + { + $this->validation = new Validation(); + + $this->setInputfield(); + + $this->validateFieldvalue(); + + $this->setFieldname(); + + $this->setValidationResult(); + + $this->setErrorMesagges(); + } + } + + private function setInputfield() + { + $this->fieldValue = $this->ci->input->post( $this->key ); + } + + private function validateFieldvalue() + { + $this->validation->validate( $this->fieldValue, $this->validationField ); + } + + private function setFieldname() + { + $this->fieldname = $this->validationField['fieldname']; + } + + private function setErrorMesagges() + { + $this->errorMessages[ $this->fieldname ] = $this->validation->getErrorMessages(); + } + + private function setValidationResult() + { + if( $this->validationResult === FALSE ) return; + + $this->validationResult = $this->validation->getValidationResult(); + } +} \ No newline at end of file diff --git a/application/libraries/validation/inputValidations/LoginValidation.php b/application/libraries/validation/inputValidations/LoginValidation.php new file mode 100644 index 0000000..c9f1c76 --- /dev/null +++ b/application/libraries/validation/inputValidations/LoginValidation.php @@ -0,0 +1,28 @@ + [ + 'validations' => 'required|alphaNumericSpaces', + 'fieldname' => 'gebruikersnaam' + ], + 'password' => [ + 'validations' => 'required', + 'fieldname' => 'wachtwoord' + ] + ]; + + public function __construct() + { + parent::__construct(); + } +} \ No newline at end of file diff --git a/application/libraries/validation/inputValidations/OrganisationValidation.php b/application/libraries/validation/inputValidations/OrganisationValidation.php new file mode 100644 index 0000000..1002d32 --- /dev/null +++ b/application/libraries/validation/inputValidations/OrganisationValidation.php @@ -0,0 +1,47 @@ + [ + 'validations' => 'required|alphaNumeric', + 'fieldname' => 'brinnummer' + ], + 'naam' => [ + 'validations' => 'required', + 'fieldname' => 'Organisatienaam' + ], + 'naamkort' => [ + 'validations' => 'required', + 'fieldname' => 'Organisatienaam (kort)' + ], + 'vestiging' => [ + 'validations' => 'required|alphaNumeric|unique', + 'fieldname' => 'vestigingsnummer', + 'model' => 'organisations_model', + 'field' => 'vestiging' + ], + 'email' => [ + 'validations' => 'email', + 'fieldname' => 'email' + ] + ]; + + public function __construct() + { + parent::__construct(); + + $this->ci =& get_instance(); + } + +} \ No newline at end of file diff --git a/application/libraries/validation/inputValidations/PasswordRequestValidation.php b/application/libraries/validation/inputValidations/PasswordRequestValidation.php new file mode 100644 index 0000000..5e82827 --- /dev/null +++ b/application/libraries/validation/inputValidations/PasswordRequestValidation.php @@ -0,0 +1,28 @@ + [ + 'validations' => 'required|alphaNumericSpaces', + 'fieldname' => 'gebruikersnaam' + ], + 'email' => [ + 'validations' => 'required|email', + 'fieldname' => 'email' + ] + ]; + + public function __construct() + { + parent::__construct(); + } +} \ No newline at end of file diff --git a/application/libraries/validation/inputValidations/UserValidation.php b/application/libraries/validation/inputValidations/UserValidation.php new file mode 100644 index 0000000..dd35940 --- /dev/null +++ b/application/libraries/validation/inputValidations/UserValidation.php @@ -0,0 +1,44 @@ + [ + 'validations' => 'required|alphaNumericSpaces', + 'fieldname' => 'firstname' + ], + 'lastname' => [ + 'validations' => 'required|alphaNumericSpaces', + 'fieldname' => 'lastname' + ], + 'email' => [ + 'validations' => 'required|email|unique', + 'fieldname' => 'email', + 'model' => 'User_model', + 'field' => 'email' + ], + 'username' => [ + 'validations' => 'required|alphanumeric|minlength-4|unique', + 'fieldname' => 'username', + 'model' => 'User_model', + 'field' => 'username' + ] + ]; + + public function __construct() + { + parent::__construct(); + + $this->ci =& get_instance(); + } +} \ No newline at end of file diff --git a/application/models/beheer/User_model.php b/application/models/beheer/User_model.php index 151fbb9..2ca8d67 100644 --- a/application/models/beheer/User_model.php +++ b/application/models/beheer/User_model.php @@ -23,4 +23,19 @@ class User_model extends MY_Model { return $user; } + public function checkUnique($recordID, $field, $fieldValue) + { + $this->sql = " + SELECT * + FROM users + WHERE ucase( {$field} ) = '{strtoupper($fieldValue)}' + AND id != {$recordID} "; + + $result = $this->get_query_row(); + + if( ! $result ) return TRUE; + + return FALSE; + } + } \ No newline at end of file diff --git a/application/models/eloquent/User.php b/application/models/eloquent/User.php index af24183..ad399dc 100644 --- a/application/models/eloquent/User.php +++ b/application/models/eloquent/User.php @@ -45,5 +45,21 @@ class User extends BaseModel { return TRUE; } + public static function saveUser( $userID, $user ) + { + if( $userID == 0) + { + $user = User::create($user); + } + else + { + $user = User::whereId($userID); + + $user->update($user); + } + + return $user->id; + } + } \ No newline at end of file diff --git a/application/views/pages/home_view.php b/application/views/pages/home_view.php new file mode 100644 index 0000000..68d880f --- /dev/null +++ b/application/views/pages/home_view.php @@ -0,0 +1,30 @@ +
+
+ +
+
+
+

+ pagename ?> +

+
+
+
+ subpages as $subpage ): ?> +
+
+
+
+ pagename ?> +
+
+
+
+ +
+
+
+
+ +
+
\ No newline at end of file diff --git a/application/views/partials/_topmenu.php b/application/views/partials/_topmenu.php new file mode 100644 index 0000000..13bf616 --- /dev/null +++ b/application/views/partials/_topmenu.php @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/composer.json b/composer.json index 786cb7f..c6b0327 100755 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ }, "autoload": { "classmap": [ - "application/models/eloquent" + "application/models/eloquent", + "application/libraries/validation" ] }, "suggest": { diff --git a/public/js/lib/dataservices/userDataservice.js b/public/js/lib/dataservices/userDataservice.js index b423c9e..ea02614 100644 --- a/public/js/lib/dataservices/userDataservice.js +++ b/public/js/lib/dataservices/userDataservice.js @@ -27,6 +27,13 @@ dbo.userDataService = { console.log( data ); callback( data ); } ) + }, + + saveUser: function ( submitDATA, callback ) { + $.post( 'index.php/beheer/usersController/saveUser', submitDATA, 'json' ) + .done( function ( data ) { + callback( data ); + } ) } diff --git a/public/js/lib/modules/login/loginModule.js b/public/js/lib/modules/login/loginModule.js index 043ac6d..1775114 100644 --- a/public/js/lib/modules/login/loginModule.js +++ b/public/js/lib/modules/login/loginModule.js @@ -55,7 +55,7 @@ dbo.loginService = { }, notifyWrongPassword: function () { - notifyMe( { + tellMe( { title : 'Login fout', message: 'Gebruikersnaam / wachtwoord komen niet overeen' } ); diff --git a/public/js/lib/modules/modals/userModal.js b/public/js/lib/modules/modals/userModal.js index e9327db..f9cc639 100644 --- a/public/js/lib/modules/modals/userModal.js +++ b/public/js/lib/modules/modals/userModal.js @@ -45,6 +45,7 @@ $( function () { dbo.userDataService.checkUsername() var submitDATA = { + id : vm.id, firstname : vm.firstname(), middlename: vm.middlename(), lastname : vm.lastname(), @@ -53,7 +54,22 @@ $( function () { role : vm.role() } + dbo.userDataService.saveUser( submitDATA, function ( data ) { + if ( data.succes ) { + tellMe( { + title : 'gebruiker aangemaakt', + message : 'gebuiker is succesvol aangemaakt', + modalClass: 'modal-success' + } ); + return; + } + tellMe({ + title : 'fout aanmaken gebruiker', + message : data.errorMessages, + modalClass: 'modal-danger' + }); + } ) }, @@ -62,7 +78,7 @@ $( function () { var vm = dbo.editUserModal; vm.id = user.id || 0; - vm.vm.firstname( user.firstname || '' ); + vm.firstname( user.firstname || '' ); vm.lastname( user.firstname || '' ); vm.middlename( user.firstname || '' ); vm.username( user.username || '' ); diff --git a/public/js/lib/modules/specials/notifyModule.js b/public/js/lib/modules/specials/notifyModule.js index dfa7d19..a671bc2 100644 --- a/public/js/lib/modules/specials/notifyModule.js +++ b/public/js/lib/modules/specials/notifyModule.js @@ -5,7 +5,7 @@ * Time: 17:08 */ -var notifyMe = function ( options, callback ) { +var tellMe = function ( options, callback ) { dbo.notifyModel.init( options, callback ); }