diff --git a/authentication/auth-bundle/AuthBundle.php b/authentication/auth-bundle/AuthBundle.php new file mode 100644 index 00000000..022e5f0b --- /dev/null +++ b/authentication/auth-bundle/AuthBundle.php @@ -0,0 +1,7 @@ +users = new ArrayCollection(); + $this->acl_attributes = []; + } + + public function setID($id) + { + // example ROLE_SUPER_ADMIN, ROLE_CASHIER, etc + $this->id = $id; + return $this; + } + + public function getID() + { + return $this->id; + } + + public function setName($name) + { + $this->name = $name; + return $this; + } + + public function getName() + { + return $this->name; + } + + public function getUsers() + { + return $this->users; + } + + public function getUsersCount() + { + return $this->users->count(); + } + + public function isSuperAdmin() + { + if ($this->id == self::SUPER_ADMIN) + return true; + + return false; + } + + // TODO: shift out ACL stuff to its own class + public function clearACLAttributes() + { + $this->acl_attributes = []; + return $this; + } + + public function getACLAttributes() + { + return $this->acl_attributes; + } + + public function addACLAccess($attribute) + { + $this->acl_attributes[$attribute] = true; + return $this; + } + + public function hasACLAccess($attribute) + { + // if it's super admin, they always have access + if ($this->isSuperAdmin()) + return true; + + // check ACL attributes + if (isset($this->acl_attributes[$attribute]) && $this->acl_attributes[$attribute]) + return true; + + return false; + } +} diff --git a/authentication/auth-bundle/Entity/APIUser.php b/authentication/auth-bundle/Entity/APIUser.php new file mode 100644 index 00000000..1cfc3291 --- /dev/null +++ b/authentication/auth-bundle/Entity/APIUser.php @@ -0,0 +1,156 @@ +setAPIKey($this->generateAPIKey()) + ->setSecretKey($this->generateSecretKey()); + + // set date created + $this->date_create = new DateTime(); + + $this->roles = new ArrayCollection(); + } + + public function getID() + { + return $this->id; + } + + public function setAPIKey($api_key) + { + $this->api_key = $api_key; + return $this; + } + + public function getAPIKey() + { + return $this->api_key; + } + + public function setSecretKey($key) + { + $this->secret_key = $key; + return $this; + } + + public function getSecretKey() + { + return $this->secret_key; + } + + public function setName($name) + { + $this->name = $name; + return $this; + } + + public function getName() + { + return $this->name; + } + + public function getRoles() + { + $str_roles = []; + foreach ($this->roles as $role) + $str_roles[] = $role->getID(); + + return $str_roles; + } + + public function getRoleObjects() + { + return $this->roles; + } + + public function getDateCreate() + { + return $this->date_create; + } + + public function getPassword() + { + // we don't need this for API + return 'notneeded'; + } + + public function getSalt() + { + return null; + } + + public function getUsername() + { + // since it's an api, the api key IS the username + return $this->api_key; + } + + public function eraseCredentials() + { + return; + } + + public function generateAPIKey() + { + return $this->generateKey('api'); + } + + public function generateSecretKey() + { + return $this->generateKey('secret'); + } + + protected function generateKey($prefix = '') + { + return md5(uniqid($prefix, true)); + } +} + diff --git a/authentication/auth-bundle/Entity/Role.php b/authentication/auth-bundle/Entity/Role.php new file mode 100644 index 00000000..168b127d --- /dev/null +++ b/authentication/auth-bundle/Entity/Role.php @@ -0,0 +1,120 @@ +users = new ArrayCollection(); + $this->acl_attributes = []; + } + + public function setID($id) + { + // example ROLE_SUPER_ADMIN, ROLE_CASHIER, etc + $this->id = $id; + return $this; + } + + public function getID() + { + return $this->id; + } + + public function setName($name) + { + $this->name = $name; + return $this; + } + + public function getName() + { + return $this->name; + } + + public function getUsers() + { + return $this->users; + } + + public function getUsersCount() + { + return $this->users->count(); + } + + public function isSuperAdmin() + { + if ($this->id == self::SUPER_ADMIN) + return true; + + return false; + } + + // TODO: shift out ACL stuff to its own class + public function clearACLAttributes() + { + $this->acl_attributes = []; + return $this; + } + + public function getACLAttributes() + { + return $this->acl_attributes; + } + + public function addACLAccess($attribute) + { + $this->acl_attributes[$attribute] = true; + return $this; + } + + public function hasACLAccess($attribute) + { + // if it's super admin, they always have access + if ($this->isSuperAdmin()) + return true; + + // check ACL attributes + if (isset($this->acl_attributes[$attribute]) && $this->acl_attributes[$attribute]) + return true; + + return false; + } +} diff --git a/authentication/auth-bundle/Entity/User.php b/authentication/auth-bundle/Entity/User.php new file mode 100644 index 00000000..88cb8cc9 --- /dev/null +++ b/authentication/auth-bundle/Entity/User.php @@ -0,0 +1,337 @@ +roles = new ArrayCollection(); + $this->hubs = new ArrayCollection(); + $this->job_orders_created = new ArrayCollection(); + $this->job_orders_assigned = new ArrayCollection(); + $this->tickets = new ArrayCollection(); + $this->enabled = true; + } + + public function getID() + { + return $this->id; + } + + public function setUsername($username) + { + $this->username = $username; + return $this; + } + + public function getUsername() + { + return $this->username; + } + + public function setPassword($password) + { + $this->password = $password; + return $this; + } + + public function getPassword() + { + return $this->password; + } + + public function setSalt($salt) + { + // do nothing + return $this; + } + + public function getSalt() + { + return null; + } + + public function addRole(Role $role) + { + $this->roles->add($role); + return $this; + } + + public function clearRoles() + { + $this->roles->clear(); + return $this; + } + + public function getRoles() + { + // has to return set of strings because symfony is trying to move away from role objects + $str_roles = []; + foreach ($this->roles as $role) + $str_roles[] = $role->getID(); + + return $str_roles; + } + + public function getRoleObjects() + { + return $this->roles; + } + + public function addHub(Hub $hub) + { + $this->hubs->add($hub); + return $this; + } + + public function clearHubs() + { + $this->hubs->clear(); + return $this; + } + + public function getHubs() + { + $str_hubs = []; + foreach ($this->hubs as $hub) + $str_hubs[] = $hub->getID(); + + return $str_hubs; + } + + public function getHubObjects() + { + return $this->hubs; + } + + public function eraseCredentials() + { + return $this; + } + + public function isAccountNonExpired() + { + return true; + } + + public function isAccountNonLocked() + { + return true; + } + + public function isCredentialsNonExpired() + { + return true; + } + + public function setEnabled($enabled = true) + { + $this->enabled = $enabled; + return $this; + } + + public function isEnabled() + { + return $this->enabled; + } + + public function serialize() + { + return serialize([ + $this->id, + $this->username, + $this->password, + $this->enabled, + ]); + } + + public function unserialize($serial) + { + list ( + $this->id, + $this->username, + $this->password, + $this->enabled, + ) = unserialize($serial); + } + + public function setFirstName($name) + { + $this->first_name = $name; + return $this; + } + + public function getFirstName() + { + return $this->first_name; + } + + public function setLastName($name) + { + $this->last_name = $name; + return $this; + } + + public function getLastName() + { + return $this->last_name; + } + + public function getFullName() + { + return $this->first_name . ' ' . $this->last_name; + } + + public function setContactNumber($num) + { + $this->contact_num = $num; + return $this; + } + + public function getContactNumber() + { + return $this->contact_num; + } + + public function setEmail($email = null) + { + $this->email = $email; + return $this; + } + + public function getEmail() + { + return $this->email; + } + + public function isSuperAdmin() + { + foreach ($this->roles as $role) + { + if ($role->isSuperAdmin()) + return true; + } + + return false; + } + + public function getJobOrdersCreated() + { + return $this->job_orders_created; + } + + public function getJobOrdersAssigned() + { + return $this->job_orders_assigned; + } + + public function getTickets() + { + return $this->tickets; + } + + public function getInvoices() + { + return $this->invoices; + } +} diff --git a/config/bundles.php b/config/bundles.php index db1fdb73..d05a5a86 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -14,4 +14,5 @@ return [ Catalyst\APIBundle\CatalystAPIBundle::class => ['all' => true], // DataDog\AuditBundle\DataDogAuditBundle::class => ['all' => true], + Authentication\AuthBundle\Authbundle::class => ['all' => true], ];