Fix API key creation logic

This commit is contained in:
Dane Everitt 2020-03-28 16:06:36 -07:00
parent ff49165447
commit e4e5dea6b8
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 68 additions and 57 deletions

View File

@ -53,7 +53,7 @@ class ApiKey extends Validable
* @var array * @var array
*/ */
protected $casts = [ protected $casts = [
'allowed_ips' => 'json', 'allowed_ips' => 'array',
'user_id' => 'int', 'user_id' => 'int',
'r_' . AdminAcl::RESOURCE_USERS => 'int', 'r_' . AdminAcl::RESOURCE_USERS => 'int',
'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int', 'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int',
@ -99,7 +99,8 @@ class ApiKey extends Validable
'identifier' => 'required|string|size:16|unique:api_keys,identifier', 'identifier' => 'required|string|size:16|unique:api_keys,identifier',
'token' => 'required|string', 'token' => 'required|string',
'memo' => 'required|nullable|string|max:500', 'memo' => 'required|nullable|string|max:500',
'allowed_ips' => 'nullable|json', 'allowed_ips' => 'nullable|array',
'allowed_ips.*' => 'string',
'last_used_at' => 'nullable|date', 'last_used_at' => 'nullable|date',
'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3', 'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3',
'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'integer|min:0|max:3', 'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'integer|min:0|max:3',

View File

@ -140,7 +140,12 @@ abstract class Validable extends Model
} }
return $this->getValidator()->setData( return $this->getValidator()->setData(
$this->toArray() // Trying to do self::toArray() here will leave out keys based on the whitelist/blacklist
// for that model. Doing this will return all of the attributes in a format that can
// properly be validated.
$this->addCastAttributesToArray(
$this->getAttributes(), $this->getMutatedAttributes()
)
)->passes(); )->passes();
} }
} }

View File

@ -46,8 +46,9 @@ export default () => {
}; };
return ( return (
<div className={'my-10 flex'}> <div className={'my-10'}>
<FlashMessageRender byKey={'account'} className={'mb-4'}/> <FlashMessageRender byKey={'account'} className={'mb-4'}/>
<div className={'flex'}>
<ContentBox title={'Create API Key'} className={'flex-1'}> <ContentBox title={'Create API Key'} className={'flex-1'}>
<CreateApiKeyForm onKeyCreated={key => setKeys(s => ([ ...s!, key ]))}/> <CreateApiKeyForm onKeyCreated={key => setKeys(s => ([ ...s!, key ]))}/>
</ContentBox> </ContentBox>
@ -75,7 +76,10 @@ export default () => {
</p> </p>
: :
keys.map(key => ( keys.map(key => (
<div key={key.identifier} className={'grey-row-box bg-neutral-600 mb-2 flex items-center'}> <div
key={key.identifier}
className={'grey-row-box bg-neutral-600 mb-2 flex items-center'}
>
<FontAwesomeIcon icon={faKey} className={'text-neutral-300'}/> <FontAwesomeIcon icon={faKey} className={'text-neutral-300'}/>
<div className={'ml-4 flex-1'}> <div className={'ml-4 flex-1'}>
<p className={'text-sm'}>{key.description}</p> <p className={'text-sm'}>{key.description}</p>
@ -103,5 +107,6 @@ export default () => {
} }
</ContentBox> </ContentBox>
</div> </div>
</div>
); );
}; };