Skip to content

Commit

Permalink
Merge branch 'master' into #355-2fa
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/HMS/Entities/User.php
#	app/Http/Kernel.php
  • Loading branch information
dpslwk committed May 25, 2019
2 parents 4f104f6 + f35cf75 commit f72c759
Show file tree
Hide file tree
Showing 61 changed files with 1,294 additions and 243 deletions.
2 changes: 1 addition & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.hostname = "hmsdev.nottingtest.org.uk"

config.vm.provider :virtualbox do |vb|
vb.customize ['modifyvm', :id, '--memory', '2048']
vb.customize ['modifyvm', :id, '--memory', '4096']
vb.customize ['modifyvm', :id, '--natdnsproxy1', 'on']
vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
vb.customize ['guestproperty', 'set', :id,
Expand Down
2 changes: 1 addition & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function unauthorized($request, AuthorizationException $exception)
return response()->json(['error' => 'Unauthorized.'], IlluminateResponse::HTTP_FORBIDDEN);
}

flash('Unauthorized', 'error');
flash('Unauthorized')->error();

return redirect()->route('home');
}
Expand Down
11 changes: 11 additions & 0 deletions app/HMS/Doctrine/CarbonDateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace HMS\Doctrine;

class CarbonDateType extends CarbonType
{
/**
* {@inheritdoc}
*/
protected $getFormatString = 'getDateFormatString';
}
11 changes: 11 additions & 0 deletions app/HMS/Doctrine/CarbonTimeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace HMS\Doctrine;

class CarbonTimeType extends CarbonType
{
/**
* {@inheritdoc}
*/
protected $getFormatString = 'getTimeFormatString';
}
76 changes: 72 additions & 4 deletions app/HMS/Doctrine/CarbonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,88 @@

namespace HMS\Doctrine;

use DateTime;
use DateTimeZone;
use Carbon\Carbon;
use InvalidArgumentException;
use Doctrine\DBAL\Types\DateTimeType;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Platforms\AbstractPlatform;

class CarbonType extends DateTimeType
{
/**
* @var null|\DateTimeZone
*/
private static $utc = null;

/**
* Function name to call on AbstractPlatform to get the relevent string format.
*
* @var string
*/
protected $getFormatString = 'getDateTimeFormatString';

/**
* Converts the Datetime to UTC before storing to database.
*
* @param mixed $value
* @param AbstractPlatform $platform
*
* @return mixed|null
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}

if (is_null(self::$utc)) {
self::$utc = new \DateTimeZone('UTC');
}

if ($value instanceof DateTime) {
$value->setTimezone(self::$utc);
}

return parent::convertToDatabaseValue($value, $platform);
}

/**
* Converts the Datetime from UTC to default timezone.
*
* @param mixed $value
* @param AbstractPlatform $platform
*
* @return Carbon|null
* @throws ConversionException
* @throws InvalidArgumentException
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
$result = parent::convertToPHPValue($value, $platform);
if ($value === null || $value instanceof Carbon) {
return $value;
}

if (is_null(self::$utc)) {
self::$utc = new \DateTimeZone('UTC');
}

$converted = Carbon::createFromFormat(
$platform->{$this->getFormatString}(),
$value,
self::$utc
);
$converted->setTimezone(new DateTimeZone(date_default_timezone_get()));

if ($result instanceof \DateTime) {
return Carbon::instance($result);
if (! $converted) {
throw ConversionException::conversionFailedFormat(
$value,
$this->getName(),
$platform->{$this->getFormatString}()
);
}

return $result;
return $converted;
}
}
53 changes: 53 additions & 0 deletions app/HMS/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace HMS\Entities;

use HMS\Entities\GateKeeper\Pin;
use HMS\Entities\Banking\Account;
use Laravel\Passport\HasApiTokens;
use HMS\Entities\GateKeeper\RfidTag;
use HMS\Traits\Entities\SoftDeletable;
use HMS\Traits\Entities\Timestampable;
use LaravelDoctrine\ACL\Roles\HasRoles;
Expand Down Expand Up @@ -90,6 +92,16 @@ class User implements
*/
protected $emails;

/**
* @var null|Pin
*/
protected $pin;

/**
* @var ArrayCollection|RfidTag[]
*/
protected $rfidTags;

/**
* @var bool
*/
Expand Down Expand Up @@ -120,6 +132,7 @@ public function __construct(
$this->email = $email;
$this->roles = new ArrayCollection();
$this->emails = new ArrayCollection();
$this->rfidTags = new ArrayCollection();
$this->google2faEnable = false;
}

Expand Down Expand Up @@ -352,6 +365,46 @@ public function getEmails()
return $this->emails;
}

/**
* @return null|HMS\Entities\GateKeeper\Pin
*/
public function getPin()
{
return $this->pin;
}

/**
* @param null|HMS\Entities\GateKeeper\Pin $pin
*
* @return self
*/
public function setPin(Pin $pin)
{
$this->pin = $pin;

return $this;
}

/**
* @return RfidTag[]
*/
public function getRfidTags()
{
return $this->rfidTags;
}

/**
* @param ArrayCollection|RfidTag[] $rfidTags
*
* @return self
*/
public function setRfidTags($rfidTags)
{
$this->rfidTags = $rfidTags;

return $this;
}

/**
* @return bool
*/
Expand Down
5 changes: 3 additions & 2 deletions app/HMS/Mappings/HMS.Entities.GateKeeper.Pin.dcm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ HMS\Entities\GateKeeper\Pin:
type: integer
options:
default: 10
manyToOne:
oneToOne:
user:
targetEntity: \HMS\Entities\User
targetEntity: \HMS\Entities\User
inversedBy: pin
7 changes: 7 additions & 0 deletions app/HMS/Mappings/HMS.Entities.User.dcm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ HMS\Entities\User:
targetEntity: Profile
mappedBy: user
cascade: ["all"]
pin:
targetEntity: \HMS\Entities\GateKeeper\Pin
mappedBy: user
oneToMany:
rfidTags:
targetEntity: \HMS\Entities\GateKeeper\RfidTag
mappedBy: user
manyToOne:
account:
targetEntity: \HMS\Entities\Banking\Account
Expand Down
22 changes: 22 additions & 0 deletions app/HMS/Repositories/Doctrine/DoctrineRoleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace HMS\Repositories\Doctrine;

use HMS\Entities\Role;
use HMS\Entities\User;
use Doctrine\ORM\EntityRepository;
use HMS\Repositories\RoleRepository;
use Doctrine\Common\Collections\ArrayCollection;
Expand Down Expand Up @@ -57,6 +58,27 @@ public function findOneByEmail(string $email)
return parent::findOneByEmail($email);
}

/**
* Find all the team roles a given user has.
*
* @param User $user
*
* @return Roles[]
*/
public function findTeamsForUser(User $user)
{
$q = parent::createQueryBuilder('role')
->leftJoin('role.users', 'user')
->where('role.name LIKE :name')
->andWhere('user.id = :user_id');

$q = $q->setParameter('name', 'team.%')
->setParameter('user_id', $user->getId())
->getQuery();

return $q->getResult();
}

/**
* Store a new user in the DB.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ class DoctrineProjectRepository extends EntityRepository implements ProjectRepos
* @param User $user
* @param int $perPage
* @param string $pageName
* @param bool $active
*
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
public function paginateByUser(User $user, $perPage = 15, $pageName = 'page')
public function paginateByUser(User $user, $perPage = 15, $pageName = 'page', $active = false)
{
$q = parent::createQueryBuilder('project')
->where('project.user = :user_id');

if ($active) {
$q->andWhere('project.state = :project_state')
->orderBy('project.startDate', 'DESC')
->setParameter('project_state', ProjectState::ACTIVE);
}

$q = $q->setParameter('user_id', $user->getId())->getQuery();

return $this->paginate($q, $perPage, $pageName);
Expand Down
3 changes: 2 additions & 1 deletion app/HMS/Repositories/Members/ProjectRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ interface ProjectRepository
* @param User $user
* @param int $perPage
* @param string $pageName
* @param bool $active
*
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
public function paginateByUser(User $user, $perPage = 15, $pageName = 'page');
public function paginateByUser(User $user, $perPage = 15, $pageName = 'page', $active = false);

/**
* Return number of active projects for a user.
Expand Down
10 changes: 10 additions & 0 deletions app/HMS/Repositories/RoleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace HMS\Repositories;

use HMS\Entities\Role;
use HMS\Entities\User;
use Doctrine\Common\Collections\ArrayCollection;

interface RoleRepository
Expand Down Expand Up @@ -37,6 +38,15 @@ public function findOneByName(string $roleName);
*/
public function findOneByEmail(string $email);

/**
* Find all the team roles a given user has.
*
* @param User $user
*
* @return Roles[]
*/
public function findTeamsForUser(User $user);

/**
* Store a new user in the DB.
*
Expand Down
9 changes: 9 additions & 0 deletions app/HMS/Repositories/Tools/BookingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ public function findInductionByTool(Tool $tool);
*/
public function findMaintenanceByTool(Tool $tool);

/**
* Find bookings for a given user that are either now or in the futuer no matter the tool.
*
* @param User $user
*
* @return Booking[]
*/
public function findFutureByUser(User $user);

/**
* Save Booking to the DB.
*
Expand Down
24 changes: 24 additions & 0 deletions app/HMS/Repositories/Tools/Doctrine/DoctrineBookingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,30 @@ public function findMaintenanceByTool(Tool $tool)
return parent::findBy(['tool' => $tool, 'type' => BookingType::MAINTENANCE], ['start' => 'ASC']);
}

/**
* Find bookings for a given user that are either now or in the futuer no matter the tool.
*
* @param User $user
*
* @return Booking[]
*/
public function findFutureByUser(User $user)
{
$now = Carbon::now();

$expr = Criteria::expr();
$criteria = Criteria::create()
->where(
$expr->andX(
$expr->eq('user', $user),
$expr->gte('end', $now)
)
)
->orderBy(['start' => Criteria::ASC]);

return $this->matching($criteria)->toArray();
}

/**
* Save Booking to the DB.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function index(Request $request)
}

if (is_null($user->getAccount())) {
flash('No Account for ' . $user->getName())->error();
flash('No Account for ' . $user->getFirstname())->error();

return redirect()->route('home');
}
Expand Down
Loading

0 comments on commit f72c759

Please sign in to comment.