public function testMethods()
 {
     // Clear validations
     \model\ValidationService::Clear();
     $appSettings = $this->setupAppsettings();
     // Create app mockup object with properties
     $appMockup = new \stdClass();
     $appMockup->htmlView = new \stdClass();
     // Create ctrl helper class
     $ctrlHelper = new \model\CtrlHelperService($appMockup, $appSettings);
     // Ctrl to string method
     $CtrlString = $ctrlHelper->CtrlToString(new \controller\PageCtrl($ctrlHelper));
     $this->assertEquals("page", $CtrlString);
     // ProcessUrl method
     // $ctrlHelper->ProcessUrl('/page/leading/somewhere'); Not testable
     // Execute controller method
     $ctrlHelper->ExecuteController(new DummyClass(), "DummyMethod", ["someParam"]);
     // A new flashmessage should be added.
     $this->assertTrue(\model\FlashMessageService::DoesExist());
     // Clear flashmessage
     \model\FlashMessageService::Clear();
     $riskyFile = '/etc/passwd';
     // Load methods
     $this->assertFalse($ctrlHelper->LoadController($riskyFile));
     $this->assertFalse($ctrlHelper->LoadBLLModel($riskyFile));
     $this->assertFalse($ctrlHelper->LoadDALModel($riskyFile));
     $this->assertFalse($ctrlHelper->LoadService($riskyFile));
     $this->assertFalse($ctrlHelper->LoadView($riskyFile));
     // Create methods
     $this->assertFalse($ctrlHelper->CreateController($riskyFile));
     $this->assertFalse($ctrlHelper->CreateBLLModel($riskyFile));
     $this->assertFalse($ctrlHelper->CreateDALModel($riskyFile));
     $this->assertFalse($ctrlHelper->CreateService($riskyFile));
     $this->assertFalse($ctrlHelper->CreateView($riskyFile));
 }
Exemplo n.º 2
0
 public function Save()
 {
     // Create view
     $pageView = $this->ctrlHelper->CreateView('PageView');
     // Load file dependencies
     $this->ctrlHelper->LoadDALModel('UserDAL');
     $this->ctrlHelper->LoadDALModel('LoginDAL');
     // Create objects
     $pages = $this->ctrlHelper->CreateDALModel('PageDAL');
     $auth = $this->ctrlHelper->CreateService('AuthService');
     if ($auth->IsUserLoggedIn()) {
         // Get page info from view
         $pageInfoArray = $pageView->GetPageInfo();
         // Get logged in user
         $user = $auth->GetLoggedInUser();
         // Create new page
         $page = new \model\Page($pageInfoArray['pageId'], $pageInfoArray['header'], $pageInfoArray['content'], $user->GetUsername());
         // Check if there was validation errors
         if (!\model\ValidationService::IsValid()) {
             \model\ValidationService::ConvertErrorsToFlashMessages();
             $pageId = $page->GetPageId();
         } else {
             // Generate slug
             $page->GenerateSlug();
             $pageId = $pages->Save($page);
             \model\FlashMessageService::Add("Sidan sparades med ett lyckat resultat.");
         }
         // Get controller name
         $ctrlName = $this->ctrlHelper->CtrlToString($this);
         // Redirect
         $this->ctrlHelper->RedirectTo($ctrlName . "/show/" . $pageId . '/' . $page->GetSlug());
     }
 }
 private function ProcessActions()
 {
     // Try to register
     try {
         // If user wants to register
         if ($this->registrationView->UserWantsToRegister()) {
             // Get Registration attempt
             $registrationAttemptArray = $this->registrationView->GetRegistrationAttempt();
             // Create new userRegistration model from registration attempt
             $userRegistrationAttempt = new \model\UserRegistration($registrationAttemptArray['username'], $registrationAttemptArray['password'], $registrationAttemptArray['passwordRepeat']);
             // If there are no validation errors, proceed.
             if (\model\ValidationService::IsValid()) {
                 // Create new user
                 $newUser = new \model\User(null, $userRegistrationAttempt->GetUserName(), $userRegistrationAttempt->GetPassword());
                 // Add user in DAL
                 $this->users->Add($newUser);
                 // Set new message to display for user.
                 \model\FlashMessageService::Set('Registered new user.');
                 // Store last login uname for login page
                 $this->auth->SetLoginUsername($newUser);
                 // New user registered successfully. Redirect to login page
                 $this->appController->ReloadPage();
             }
         }
     } catch (\Exception $exception) {
         // Store exceptions in applications exceptions container model
         \model\ExceptionsService::AddException($exception);
     }
     // Return registration failure
     return false;
 }
Exemplo n.º 4
0
 public function testConstructorWithFaultyValues()
 {
     $user = new User("not an int", "someusername", "firstname", "surname", "password", true, true, "", true);
     // Should not be valid
     $this->assertFalse(\model\ValidationService::IsValid());
     // Clear validations
     \model\ValidationService::Clear();
     $user = new User(12, "someusernameweawioeawioeuawoiduioawuoiawioeawoidjioawjeoiawjdoiajwoe", "firstname", "surname", "password", true, true, "", true);
     // Should not be valid
     $this->assertFalse(\model\ValidationService::IsValid());
     // Clear validations
     \model\ValidationService::Clear();
     $user = new User(12, "someusername", "firstnameweawioeawioeuawoiduioawuoiawioeawoidjioawjeoiawjdoiajwoe", "surname", "password", true, true, "", true);
     // Should not be valid
     $this->assertFalse(\model\ValidationService::IsValid());
     // Clear validations
     \model\ValidationService::Clear();
     $user = new User(12, "someusername", "firstnameweawioeawioeuawoiduioawuoiawioeawoidjioawjeoiawjdoiajwoe", "surname", "password", true, true, "", true);
     // Should not be valid
     $this->assertFalse(\model\ValidationService::IsValid());
     // Clear validations
     \model\ValidationService::Clear();
     $user = new User(12, "someusername", "firstname", "surnameweawioeawioeuawoiduioawuoiawioeawoidjioawjeoiawjdoiajwoe", "password", true, true, "", true);
     // Should not be valid
     $this->assertFalse(\model\ValidationService::IsValid());
     // Clear validations
     \model\ValidationService::Clear();
     $user = new User(12, "someusername", "firstname", "surname", "passwordweawioeawioeuawoiduioawuoiawioeawoidjioawjeoiawjdoiajwoe", true, true, "", true);
     // Should not be valid
     $this->assertFalse(\model\ValidationService::IsValid());
     // Clear validations
     \model\ValidationService::Clear();
 }
 public function testConstructor()
 {
     // Clear validations
     \model\ValidationService::Clear();
     $appSettings = new AppSettings(['defaultController' => 'SomeCtrlerjaoijaiowjeioawjieojawoijtioajwirjaiwjeipajwpejapwepoawopr', 'defaultMethod' => 'Index', 'controllerPath' => '../app/controller/', 'controllerNamespace' => 'controller\\', 'modelPath' => '../app/model/', 'modelNamespace' => 'model\\', 'viewPath' => '../app/view/', 'viewNamespace' => 'view\\']);
     // Should not be valid
     $this->assertFalse(\model\ValidationService::IsValid());
     // Clear validations
     \model\ValidationService::Clear();
     $appSettings = new AppSettings(['defaultController' => 'SomeCtrl', 'defaultMethod' => 'Indexrjaoijaiowjeioawjieojawoijtioajwirjaiwjeipajwpejapwepoawopr', 'controllerPath' => '../app/controller/', 'controllerNamespace' => 'controller\\', 'modelPath' => '../app/model/', 'modelNamespace' => 'model\\', 'viewPath' => '../app/view/', 'viewNamespace' => 'view\\']);
     // Should not be valid
     $this->assertFalse(\model\ValidationService::IsValid());
     // Clear validations
     \model\ValidationService::Clear();
     $appSettings = new AppSettings(['defaultController' => 'SomeCtrl', 'defaultMethod' => 'Index', 'controllerPath' => '/var/temp/someEvilfile.php', 'controllerNamespace' => 'controller\\', 'modelPath' => '/etc/passwd', 'modelNamespace' => 'model\\', 'viewPath' => '/etc/sudoers.d', 'viewNamespace' => 'view\\']);
     // Should be valid but still secure
     $this->assertTrue(\model\ValidationService::IsValid());
     $this->assertEquals('../app/someEvilfile.php/', $appSettings->GetControllerPath());
     $this->assertEquals('../app/passwd/', $appSettings->GetModelPath());
     $this->assertEquals('../app/sudoers.d/', $appSettings->GetViewPath());
     // Check that the rest of the get methods return correct values
     $this->assertEquals('SomeCtrl', $appSettings->GetDefaultController());
     $this->assertEquals('Index', $appSettings->GetDefaultMethod());
     $this->assertEquals('controller\\', $appSettings->GetControllerNamespace());
     $this->assertEquals('model\\', $appSettings->GetModelNamespace());
     $this->assertEquals('view\\', $appSettings->GetViewNamespace());
 }
Exemplo n.º 6
0
 public function testMethods()
 {
     // Clear validations
     \model\ValidationService::Clear();
     $page = new Page(12, 'Really nice HEÄDER', 'content', 'authorName');
     $page->GenerateSlug();
     $this->assertEquals("really-nice-header", $page->GetSlug());
 }
 public function ProcessActions()
 {
     // Try to authenticate
     try {
         // If user is logged in and wants to logout
         if ($this->formView->UserWantsToLogout() && $this->auth->IsUserLoggedIn()) {
             $this->Logout();
         } else {
             if ($this->formView->UserWantsToLogin() && !$this->auth->IsUserLoggedIn()) {
                 // Get login attempt
                 $loginAttemptArray = $this->formView->GetLoginAttempt();
                 // Create new user from login attempt
                 $loginAttemptUser = new \model\User(NULL, $loginAttemptArray['username'], $loginAttemptArray['password'], false);
                 // If there are no validation errors, proceed.
                 if (\model\ValidationService::IsValid()) {
                     // Try to authenticate user
                     if ($user = $this->auth->Authenticate($loginAttemptUser)) {
                         $this->DoLoginSuccess($user);
                     } else {
                         // The user was denied access
                         throw new \Exception("Wrong name or password");
                     }
                 }
             } else {
                 if ($this->formView->IsLoginSavedOnClient() && !$this->auth->IsUserLoggedIn()) {
                     // Get client login info
                     $userInfoArray = $this->formView->GetLoginSavedOnClient();
                     $user = new \model\user(NULL, $userInfoArray['username'], NULL, false, false, $userInfoArray['token'], false);
                     if ($this->auth->AuthenticatePersistent($user)) {
                         $this->DoLoginSuccess($user);
                     }
                 }
             }
         }
     } catch (\Exception $exception) {
         // Store exceptions in applications exceptions container model
         \Model\ExceptionsService::AddException($exception);
     }
     // Return login failure
     return false;
 }
 public function GetHTML()
 {
     $messageToUser = '';
     // Get exception messages if there are any.
     if (\model\ExceptionsService::HasExceptions()) {
         foreach (\model\ExceptionsService::GetAllExceptionMessages() as $message) {
             $messageToUser .= $message . "<br>";
         }
     } else {
         if (!\model\ValidationService::IsValid()) {
             foreach (\model\ValidationService::GetValidationErrors() as $message) {
                 $messageToUser .= $message . "<br>";
             }
         } else {
             if (\model\FlashMessageService::DoesExist()) {
                 $messageToUser .= \model\FlashMessageService::Get();
             }
         }
     }
     // Get output
     return $this->GetRegisterFormOutput($messageToUser);
 }
Exemplo n.º 9
0
 public function GetHTML()
 {
     $messageToUser = '';
     // Get exception messages if there are any.
     if (\model\ExceptionsService::HasExceptions()) {
         $messageToUser = \model\ExceptionsService::GetLastExceptionMessage();
     } else {
         if (!\model\ValidationService::IsValid()) {
             foreach (\model\ValidationService::GetValidationErrors() as $message) {
                 $messageToUser .= $message . "<br>";
             }
         } else {
             if (\model\FlashMessageService::DoesExist()) {
                 $messageToUser = \model\FlashMessageService::Get();
             }
         }
     }
     // Get login or logout form output
     return $this->auth->IsUserLoggedIn() ? $this->GetLogoutFormOutput($messageToUser) : $this->GetLoginFormOutput($messageToUser);
 }
Exemplo n.º 10
0
 public function Login()
 {
     // Create view
     $loginView = $this->ctrlHelper->CreateView('LoginView');
     // Load file dependencies
     $this->ctrlHelper->LoadBLLModel('User');
     $this->ctrlHelper->LoadDALModel('UserDAL');
     $this->ctrlHelper->LoadDALModel('LoginDAL');
     $this->ctrlHelper->LoadService('UserClientService');
     // Create Auth service
     $auth = $this->ctrlHelper->CreateService('AuthService');
     // If user is already logged in
     if ($auth->IsUserLoggedIn()) {
         $this->ctrlHelper->RedirectTo("page");
     }
     // If user wants to login
     if ($loginView->UserWantsToLogin()) {
         // Get login attempt
         $loginAttemptArray = $loginView->GetLoginAttempt();
         // Create new user from login attempt
         $loginAttemptUser = new \model\User(NULL, $loginAttemptArray['username'], "", "", $loginAttemptArray['password'], false);
         // If there are no validation errors, proceed.
         if (\model\ValidationService::IsValid()) {
             // Try to authenticate user
             if ($user = $auth->Authenticate($loginAttemptUser)) {
                 // Store logged in user object in sessions cookie
                 $auth->KeepUserLoggedInForSession($user);
                 if ($loginView->UserWantsLoginToBeRemembered()) {
                     // Save persistent login on server
                     $auth->SaveLoginOnServer($user);
                     $loginView->SaveLoginOnClient($user);
                 }
                 \model\FlashMessageService::Add("Inloggningen lyckades.");
                 $this->ctrlHelper->RedirectTo("page/show");
             } else {
                 // The user was denied access
                 \model\FlashMessageService::Add("Fel användarnamn eller lösenord", "warning");
             }
         } else {
             // Move errors to flash messages, witch will be displayed for user.
             \model\ValidationService::ConvertErrorsToFlashMessages();
         }
     }
     $this->ctrlHelper->RedirectTo($this);
 }
 public function testIsValidString()
 {
     // Clear validations
     \model\ValidationService::Clear();
     $modelBll = new ModelBLL();
     // Assert string value
     $this->assertEquals(true, $modelBll->IsValidString("someName", "string"));
     // Assert int value
     $this->assertEquals(true, $modelBll->IsValidString("someName", 1));
     // Assert float value
     $this->assertEquals(true, $modelBll->IsValidString("someName", 1.5));
     // Assert object value
     $this->assertEquals(false, $modelBll->IsValidString("someName", new \stdClass()));
     // Assert bool value
     $this->assertEquals(false, $modelBll->IsValidString("someName", false));
     // Assert null value
     $this->assertEquals(false, $modelBll->IsValidString("someName", null));
     // Assert options
     $this->assertEquals(false, $modelBll->IsValidString("someName", "string", ['maxLength' => 2]));
     $this->assertEquals(false, $modelBll->IsValidString("someName", "string", ['minLength' => 20]));
     $this->assertEquals(false, $modelBll->IsValidString("someName", "<>>><><£\$@£"));
     $this->assertEquals(false, $modelBll->IsValidString("someName", "abc123", ['regex' => '/[^a-z]/i']));
     // Should not be valid
     $this->assertFalse(\model\ValidationService::IsValid());
 }