mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Объединены сущности для авторизации посредством JWT токенов и токенов, выданных через oAuth2.
Все действия, связанные с аккаунтами, теперь вызываются через url `/api/v1/accounts/<id>/<action>`. Добавлена вменяемая система разграничения прав на основе RBAC. Теперь oAuth2 токены генерируются как случайная строка в 40 символов длинной, а не UUID. Исправлен баг с неправильным временем жизни токена в ответе успешного запроса аутентификации. Теперь все unit тесты можно успешно прогнать без наличия интернета.
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
namespace common\components\Annotations;
|
||||
|
||||
class Reader extends \Minime\Annotations\Reader {
|
||||
|
||||
/**
|
||||
* Поначаду я думал кэшировать эту штуку, но потом забил, т.к. всё всё равно завернул
|
||||
* в Yii::$app->cache и как-то надобность в отдельном кэше отпала, так что пока забьём
|
||||
* и оставим как заготовку на будущее
|
||||
*
|
||||
* @return \Minime\Annotations\Interfaces\ReaderInterface
|
||||
*/
|
||||
public static function createFromDefaults() {
|
||||
return parent::createFromDefaults();
|
||||
//return new self(new \Minime\Annotations\Parser(), new RedisCache());
|
||||
}
|
||||
|
||||
}
|
@@ -1,65 +0,0 @@
|
||||
<?php
|
||||
namespace common\components\Annotations;
|
||||
|
||||
use common\components\Redis\Key;
|
||||
use common\components\Redis\Set;
|
||||
use Minime\Annotations\Interfaces\CacheInterface;
|
||||
use yii\helpers\Json;
|
||||
|
||||
class RedisCache implements CacheInterface {
|
||||
|
||||
/**
|
||||
* Generates uuid for a given docblock string
|
||||
* @param string $docblock docblock string
|
||||
* @return string uuid that maps to the given docblock
|
||||
*/
|
||||
public function getKey($docblock) {
|
||||
return md5($docblock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an annotation AST to cache
|
||||
*
|
||||
* @param string $key cache entry uuid
|
||||
* @param array $annotations annotation AST
|
||||
*/
|
||||
public function set($key, array $annotations) {
|
||||
$this->getRedisKey($key)->setValue(Json::encode($annotations))->expire(3600);
|
||||
$this->getRedisKeysSet()->add($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves cached annotations from docblock uuid
|
||||
*
|
||||
* @param string $key cache entry uuid
|
||||
* @return array cached annotation AST
|
||||
*/
|
||||
public function get($key) {
|
||||
$result = $this->getRedisKey($key)->getValue();
|
||||
if ($result === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Json::decode($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets cache
|
||||
*/
|
||||
public function clear() {
|
||||
/** @var array $keys */
|
||||
$keys = $this->getRedisKeysSet()->getValue();
|
||||
foreach ($keys as $key) {
|
||||
$this->getRedisKey($key)->delete();
|
||||
}
|
||||
}
|
||||
|
||||
private function getRedisKey(string $key): Key {
|
||||
return new Key('annotations', 'cache', $key);
|
||||
}
|
||||
|
||||
private function getRedisKeysSet(): Set {
|
||||
return new Set('annotations', 'cache', 'keys');
|
||||
}
|
||||
|
||||
}
|
@@ -6,46 +6,7 @@ use Yii;
|
||||
|
||||
class Key {
|
||||
|
||||
protected $key;
|
||||
|
||||
/**
|
||||
* @return Connection
|
||||
*/
|
||||
public function getRedis() {
|
||||
return Yii::$app->redis;
|
||||
}
|
||||
|
||||
public function getKey() : string {
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
public function getValue() {
|
||||
return $this->getRedis()->get($this->key);
|
||||
}
|
||||
|
||||
public function setValue($value) {
|
||||
$this->getRedis()->set($this->key, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function delete() {
|
||||
$this->getRedis()->del($this->key);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function exists() : bool {
|
||||
return (bool)$this->getRedis()->exists($this->key);
|
||||
}
|
||||
|
||||
public function expire(int $ttl) {
|
||||
$this->getRedis()->expire($this->key, $ttl);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function expireAt(int $unixTimestamp) {
|
||||
$this->getRedis()->expireat($this->key, $unixTimestamp);
|
||||
return $this;
|
||||
}
|
||||
private $key;
|
||||
|
||||
public function __construct(...$key) {
|
||||
if (empty($key)) {
|
||||
@@ -55,7 +16,43 @@ class Key {
|
||||
$this->key = $this->buildKey($key);
|
||||
}
|
||||
|
||||
private function buildKey(array $parts) {
|
||||
public function getRedis(): Connection {
|
||||
return Yii::$app->redis;
|
||||
}
|
||||
|
||||
public function getKey(): string {
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
public function getValue() {
|
||||
return $this->getRedis()->get($this->key);
|
||||
}
|
||||
|
||||
public function setValue($value): self {
|
||||
$this->getRedis()->set($this->key, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function delete(): self {
|
||||
$this->getRedis()->del([$this->getKey()]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function exists(): bool {
|
||||
return (bool)$this->getRedis()->exists($this->key);
|
||||
}
|
||||
|
||||
public function expire(int $ttl): self {
|
||||
$this->getRedis()->expire($this->key, $ttl);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function expireAt(int $unixTimestamp): self {
|
||||
$this->getRedis()->expireat($this->key, $unixTimestamp);
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function buildKey(array $parts): string {
|
||||
$keyParts = [];
|
||||
foreach($parts as $part) {
|
||||
$keyParts[] = str_replace('_', ':', $part);
|
||||
|
@@ -6,34 +6,34 @@ use IteratorAggregate;
|
||||
|
||||
class Set extends Key implements IteratorAggregate {
|
||||
|
||||
public function add($value) {
|
||||
$this->getRedis()->sadd($this->key, $value);
|
||||
public function add($value): self {
|
||||
$this->getRedis()->sadd($this->getKey(), $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function remove($value) {
|
||||
$this->getRedis()->srem($this->key, $value);
|
||||
public function remove($value): self {
|
||||
$this->getRedis()->srem($this->getKey(), $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function members() {
|
||||
return $this->getRedis()->smembers($this->key);
|
||||
public function members(): array {
|
||||
return $this->getRedis()->smembers($this->getKey());
|
||||
}
|
||||
|
||||
public function getValue() {
|
||||
public function getValue(): array {
|
||||
return $this->members();
|
||||
}
|
||||
|
||||
public function exists(string $value = null) : bool {
|
||||
public function exists(string $value = null): bool {
|
||||
if ($value === null) {
|
||||
return parent::exists();
|
||||
} else {
|
||||
return (bool)$this->getRedis()->sismember($this->key, $value);
|
||||
}
|
||||
|
||||
return (bool)$this->getRedis()->sismember($this->getKey(), $value);
|
||||
}
|
||||
|
||||
public function diff(array $sets) {
|
||||
return $this->getRedis()->sdiff([$this->key, implode(' ', $sets)]);
|
||||
public function diff(array $sets): array {
|
||||
return $this->getRedis()->sdiff([$this->getKey(), implode(' ', $sets)]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user