[Beeframework-svn] SF.net SVN: beeframework:[209] trunk/framework/Bee
Brought to you by:
b_hartmann,
m_plomer
From: <m_p...@us...> - 2014-09-08 13:37:26
|
Revision: 209 http://sourceforge.net/p/beeframework/code/209 Author: m_plomer Date: 2014-09-08 13:37:14 +0000 (Mon, 08 Sep 2014) Log Message: ----------- - introduced UserManagerBase Modified Paths: -------------- trunk/framework/Bee/Security/AbstractAuthenticationManager.php trunk/framework/Bee/Security/Anonymous/Filter.php trunk/framework/Bee/Security/IUserDetailsService.php trunk/framework/Bee/Security/UserDetails/Doctrine2/SimpleUser.php trunk/framework/Bee/Security/UserDetails/Doctrine2/SimpleUserBase.php trunk/framework/Bee/Security/UserDetails/Doctrine2/SimpleUserDetailsService.php trunk/framework/Bee/Security/UserDetails/Doctrine2/UserBase.php trunk/framework/Bee/Utils/SmartyMailer.php Added Paths: ----------- trunk/framework/Bee/Security/UserDetails/UserManagerBase.php Modified: trunk/framework/Bee/Security/AbstractAuthenticationManager.php =================================================================== --- trunk/framework/Bee/Security/AbstractAuthenticationManager.php 2014-09-07 09:42:46 UTC (rev 208) +++ trunk/framework/Bee/Security/AbstractAuthenticationManager.php 2014-09-08 13:37:14 UTC (rev 209) @@ -1,6 +1,6 @@ <?php /* - * Copyright 2008-2010 the original author or authors. + * Copyright 2008-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,5 +59,4 @@ public final function setClearExtraInformation($clearExtraInformation) { $this->clearExtraInformation = $clearExtraInformation; } - } \ No newline at end of file Modified: trunk/framework/Bee/Security/Anonymous/Filter.php =================================================================== --- trunk/framework/Bee/Security/Anonymous/Filter.php 2014-09-07 09:42:46 UTC (rev 208) +++ trunk/framework/Bee/Security/Anonymous/Filter.php 2014-09-08 13:37:14 UTC (rev 209) @@ -202,4 +202,3 @@ } } -?> Modified: trunk/framework/Bee/Security/IUserDetailsService.php =================================================================== --- trunk/framework/Bee/Security/IUserDetailsService.php 2014-09-07 09:42:46 UTC (rev 208) +++ trunk/framework/Bee/Security/IUserDetailsService.php 2014-09-08 13:37:14 UTC (rev 209) @@ -25,18 +25,16 @@ * </p> */ interface Bee_Security_IUserDetailsService { - + /** - * Locates the user based on the username. In the actual implementation, the search may possibly be case - * insensitive, or case insensitive depending on how the implementaion instance is configured. In this case, the - * <code>Bee_Security_IUserDetails</code> object that comes back may have a username that is of a different case - * than what was actually requested. - * + * Locates the user based on the username. In the actual implementation, the search may possibly be case + * insensitive, or case insensitive depending on how the implementaion instance is configured. In this case, the + * <code>Bee_Security_IUserDetails</code> object that comes back may have a username that is of a different case + * than what was actually requested. + * + * @param $username * @return Bee_Security_IUserDetails a fully populated user record (never <code>null</code>) - * - * @throws Bee_Security_Exception_UsernameNotFound - * @throws Bee_Persistence_Exception_DataAccess + * */ function loadUserByUsername($username); -} -?> \ No newline at end of file +} \ No newline at end of file Modified: trunk/framework/Bee/Security/UserDetails/Doctrine2/SimpleUser.php =================================================================== --- trunk/framework/Bee/Security/UserDetails/Doctrine2/SimpleUser.php 2014-09-07 09:42:46 UTC (rev 208) +++ trunk/framework/Bee/Security/UserDetails/Doctrine2/SimpleUser.php 2014-09-08 13:37:14 UTC (rev 209) @@ -1,5 +1,20 @@ <?php namespace Bee\Security\UserDetails\Doctrine2; +/* + * Copyright 2008-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /** * Simple user entity class Modified: trunk/framework/Bee/Security/UserDetails/Doctrine2/SimpleUserBase.php =================================================================== --- trunk/framework/Bee/Security/UserDetails/Doctrine2/SimpleUserBase.php 2014-09-07 09:42:46 UTC (rev 208) +++ trunk/framework/Bee/Security/UserDetails/Doctrine2/SimpleUserBase.php 2014-09-08 13:37:14 UTC (rev 209) @@ -1,8 +1,23 @@ <?php namespace Bee\Security\UserDetails\Doctrine2; +/* + * Copyright 2008-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + use \Bee_Security_IGrantedAuthority; - /** * Base class for user entities * @package Bee\Security\UserDetails\Doctrine2 @@ -41,18 +56,27 @@ $this->updateRoles(); } + /** + * @param $role + */ public function removeRole($role) { $this->getAuthorities(); unset($this->rolesTransformed[$role]); $this->updateRoles(); } + /** + * @param $role + * @return bool + */ public function hasRole($role) { return array_key_exists($role, $this->getAuthorities()); } + /** + * + */ private function updateRoles() { $this->roles = array_keys($this->rolesTransformed); } - } \ No newline at end of file Modified: trunk/framework/Bee/Security/UserDetails/Doctrine2/SimpleUserDetailsService.php =================================================================== --- trunk/framework/Bee/Security/UserDetails/Doctrine2/SimpleUserDetailsService.php 2014-09-07 09:42:46 UTC (rev 208) +++ trunk/framework/Bee/Security/UserDetails/Doctrine2/SimpleUserDetailsService.php 2014-09-08 13:37:14 UTC (rev 209) @@ -1,5 +1,22 @@ <?php namespace Bee\Security\UserDetails\Doctrine2; +/* + * Copyright 2008-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use Bee\Security\UserDetails\UserManagerBase; use Bee_Security_Exception_UsernameNotFound; use Bee_Security_IUserDetails; use Bee_Security_IUserDetailsService; @@ -10,7 +27,7 @@ * Class SimpleUserDetailsService * @package Bee\Security\UserDetails\Doctrine2 */ -class SimpleUserDetailsService implements Bee_Security_IUserDetailsService { +class SimpleUserDetailsService extends UserManagerBase implements Bee_Security_IUserDetailsService { /** * @var EntityManager @@ -101,4 +118,27 @@ $this->getEntityManager()->remove($user); $this->getEntityManager()->flush($user); } + + /** + * @param array $frmdata + * @param UserBase $user + * @return UserBase + */ + public function setRoles(array $frmdata, UserBase $user) { + /** @var SimpleUser $user */ + if ($frmdata['admin']) { + $user->addRole('ROLE_ADMINISTRATOR'); + } else { + $user->removeRole('ROLE_ADMINISTRATOR'); + } + $user->addRole('ROLE_USER'); + return $user; + } + + /** + * @return UserBase + */ + public function createUserInstance() { + return new $this->userEntityName(); + } } \ No newline at end of file Modified: trunk/framework/Bee/Security/UserDetails/Doctrine2/UserBase.php =================================================================== --- trunk/framework/Bee/Security/UserDetails/Doctrine2/UserBase.php 2014-09-07 09:42:46 UTC (rev 208) +++ trunk/framework/Bee/Security/UserDetails/Doctrine2/UserBase.php 2014-09-08 13:37:14 UTC (rev 209) @@ -1,9 +1,23 @@ <?php +namespace Bee\Security\UserDetails\Doctrine2; +/* + * Copyright 2008-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -namespace Bee\Security\UserDetails\Doctrine2; use Bee_Security_IUserDetails; - /** * Class UserBase * @package Bee\Security\UserDetails\Doctrine2 Added: trunk/framework/Bee/Security/UserDetails/UserManagerBase.php =================================================================== --- trunk/framework/Bee/Security/UserDetails/UserManagerBase.php (rev 0) +++ trunk/framework/Bee/Security/UserDetails/UserManagerBase.php 2014-09-08 13:37:14 UTC (rev 209) @@ -0,0 +1,135 @@ +<?php +namespace Bee\Security\UserDetails; +/* + * Copyright 2008-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use Bee\Security\UserDetails\Doctrine2\UserBase; +use Bee_Security_IPasswordEncoder; +use Bee_Security_Provider_ISaltSource; +use Bee_Utils_Strings; +use Exception; + +/** + * Class UserManagerBase + * @package Bee\Security\UserDetails + */ +abstract class UserManagerBase { + + /** + * Enter description here... + * + * @var Bee_Security_IPasswordEncoder + */ + private $passwordEncoder; + + /** + * Enter description here... + * + * @var Bee_Security_Provider_ISaltSource + */ + private $saltSource = null; + + /** + * @param Bee_Security_IPasswordEncoder $passwordEncoder + */ + public function setPasswordEncoder(Bee_Security_IPasswordEncoder $passwordEncoder) { + $this->passwordEncoder = $passwordEncoder; + } + + /** + * @return Bee_Security_IPasswordEncoder + */ + public function getPasswordEncoder() { + return $this->passwordEncoder; + } + + /** + * @param Bee_Security_Provider_ISaltSource $saltSource + */ + public function setSaltSource(Bee_Security_Provider_ISaltSource $saltSource) { + $this->saltSource = $saltSource; + } + + /** + * @return Bee_Security_Provider_ISaltSource + */ + public function getSaltSource() { + return $this->saltSource; + } + + public function createOrUpdateUser($frmdata) { + $user = is_numeric($frmdata['id']) ? $this->loadById($frmdata['id']) : $this->createUserInstance(); + $model['user'] = $user; + $user->setUsername($frmdata['username']); + $user->setName($frmdata['fullname']); + if (Bee_Utils_Strings::hasText($frmdata['password'])) { + if ($frmdata['password'] !== $frmdata['password2']) { + throw new Exception("Passwords do not match!"); + } + $this->setPassword($user, $frmdata['password']); + } + $user->setDisabled(filter_var($frmdata['deactivated'], FILTER_VALIDATE_BOOLEAN)); + $this->setRoles($frmdata, $user); + $this->addUser($user); + } + + /** + * @param UserBase $user + * @param $password + */ + public function setPassword(UserBase $user, $password) { + $user->setPassword($this->getPasswordEncoder()->encodePassword($password, $this->getSaltSource()->getSalt($user))); + } + + /** + * @param int $length + * @return string + */ + public function getRandomPassword($length = 8) { + $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789"; + $pass = ''; + $alphaLength = strlen($alphabet) - 1; + for ($i = 0; $i < $length; $i++) { + $n = rand(0, $alphaLength); + $pass .= $alphabet[$n]; + } + return $pass; + } + + /** + * @param $id + * @return UserBase + */ + abstract public function loadById($id); + + /** + * @param array $frmdata + * @param UserBase $user + * @return UserBase + */ + abstract public function setRoles(array $frmdata, UserBase $user); + + /** + * @param UserBase $user + * @return void + */ + abstract public function addUser(UserBase $user); + + /** + * @return UserBase + */ + abstract public function createUserInstance(); +} \ No newline at end of file Modified: trunk/framework/Bee/Utils/SmartyMailer.php =================================================================== --- trunk/framework/Bee/Utils/SmartyMailer.php 2014-09-07 09:42:46 UTC (rev 208) +++ trunk/framework/Bee/Utils/SmartyMailer.php 2014-09-08 13:37:14 UTC (rev 209) @@ -64,7 +64,10 @@ public function logMail($subjectTemplate, $bodyTemplate, array $model, $recipient=null, $sender=null) { $phpMailer = $this->instantiatePhpMailer($subjectTemplate, $bodyTemplate, $model, $recipient, $sender); if($this->messageLogFile && $phpMailer->preSend()) { - file_put_contents($this->messageLogFile, $phpMailer->getMailMIME()); + $message = $phpMailer->getMailMIME() . "\n\n"; + $message .= $phpMailer->Subject . "\n\n"; + $message .= $phpMailer->Body . "\n\n"; + file_put_contents($this->messageLogFile, $message); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |