Symfony kurzus 2014/2015 I. félév Security: authentication, authorization, user provider, role-ok, access control, FOS user bundle
Authen'ca'on Ez még csak azt dönti el, hogy a rendszer által azonosítóható-e a felhasználó Feladata: meghatározni, hogy ki nézi az adott oldalt az azonosítás több módszerrel is történhet HTTP alapú form alapú Facebook, Google, Twitter, stb. (OAuth 2.0)
Authoriza'on Ellenőrzi, hogy az azonosított felhasználó rendelkezik-e az útvonalhoz szükséges jogosultságokkal. Szerepkör (ROLE) alapú minden felhasználó rendelkezik bizonyos szerepkörökkel egy adott erőforráshoz (útvonal, objektum) való hozzáférés szerepkörhöz köthető sima Stringként kezelhetők hierarchikus felépítés
Authoriza'on példa # app/config/security.yml security: #... access_control: - { path: ^/admin, roles: ROLE_USER_IP, ip: 127.0.0.1 - { path: ^/admin, roles: ROLE_USER_HOST, host: symfony\.com$ - { path: ^/admin, roles: ROLE_USER_METHOD, methods: [POST, PUT] - { path: ^/admin, roles: ROLE_USER
Authoriza'on példa - { path: ^/admin, roles: ROLE_USER_IP, ip: 127.0.0.1 - { path: ^/admin, roles: ROLE_USER_HOST, host: symfony\.com$ - { path: ^/admin, roles: ROLE_USER_METHOD, methods: [POST, PUT] - { path: ^/admin, roles: ROLE_USER URI IP HOST METHOD ROLE /admin/user 127.0.0.1 example.com GET ROLE_USER_IP /admin/user 127.0.0.1 symfony.com GET ROLE_USER_IP /admin/user 168.0.0.1 symfony.com GET ROLE_USER_HOST /admin/user 168.0.0.1 symfony.com POST ROLE_USER_HOST /admin/user 168.0.0.1 example.com POST ROLE_USER_METHOD /admin/user 168.0.0.1 example.com GET ROLE_USER
Authoriza'on controllerben public function helloaction($name) { $securitycontext = $this->get('security.context'); if (false === $securitycontext->isgranted('role_admin')) { throw new AccessDeniedException(); /** * @Secure(roles="ROLE_ADMIN") */ public function helloaction($name) { //...
Authoriza'on objektumokra (ACL) objektumokra egyessével tudunk jogosultságot ellenőrizni pl. saját Actor adatlap szerkesztése adatbázis alapú ehhez szükséges táblák előkészítése: php app/console init:acl
Authoriza'on objektumokra (ACL) public function addactoraction(actor $actor) { // retrieving the security identity of the currently logged-in user $securitycontext = $this->get('security.context'); $user = $securitycontext->gettoken()->getuser(); $securityidentity = UserSecurityIdentity::fromAccount($user); // creating the ACL $aclprovider = $this->get('security.acl.provider'); $objectidentity = ObjectIdentity::fromDomainObject($actor); $acl = $aclprovider->createacl($objectidentity); // grant owner access $acl->insertobjectace($securityidentity, MaskBuilder::MASK_OWNER); $aclprovider->updateacl($acl);
Authoriza'on objektumokra (ACL) public function editactoraction(actor $actor) { $securitycontext = $this->get('security.context'); if (false === $securitycontext->isgranted('edit', $actor)) { throw new AccessDeniedException(); /** * @SecureParam(name="actor", permissions="edit") */ public function editactoraction(actor $actor) { //...
Access Control kifejezések - { path: ^/foo, access: "hasrole(role_foo') and hasrole(role_bar')" - { path: ^/foo, access: "isanonymus()" - { path: ^/foo, access: "isauthenticated()" /** * @Secure(roles="ROLE_FOO") * @SecureParam(name="actor", permissions="edit") * @PreAuthorize("isGranted('actor', 'EDIT')") * @PreAuthorize("hasRole('ROLE_FOO') and hasrole('b')") * @PreAuthorize("isAnonymus()") */ public function editactoraction(actor $actor) { //...
User Provider a felhasználókat szolgáltatja jöhetnek sok helyről config fájl, adatbázis, Facebook api, stb. beépítve 2 van conflig fáj, adatbázis providers: chain_provider: chain: providers: [in_memory, user_db] memory_provider: memory: users: ryan: { password: ryanpass, roles: 'ROLE_USER' admin: { password: kitten, roles: 'ROLE_ADMIN' database_provider: entity: { class: Acme\UserBundle\Entity\User, property: username
FOSUserBundle https://github.com/friendsofsymfony/fosuserbundle adatbázisbeli tárolásban segít, jellemzők: alapvető adatbázis séma regisztráció háttérműködése, formja email megerősítés jelszó emlékeztető unit tesztek telepítésről leírás: https://github.com/friendsofsymfony/ FOSUserBundle/blob/master/Resources/doc/ index.md
FOSUserBundle commandok php app/console fos:user: create testuser test@example.com password deactivate testuser activate testuser promote testuser ROLE_ADMIN demote testuser ROLE_ADMIN change-password testuser newpassword
FOSUserBundle UserManager $usermanager = $container ->get('fos_user.user_manager'); $user = $usermanager->createuser(); $user->setusername('john'); $user->setemail('john.doe@example.com'); $user->addrole("role_admin"); $usermanager->updateuser($user); $user = $usermanager->finduserbyusername('john'); $usermanager->deleteuser($user);