예제 #1
0
파일: User.php 프로젝트: pkdevboxy/df-core
 /**
  * {@inheritdoc}
  */
 public static function updateInternal($id, $record, $params = [])
 {
     if (empty($record)) {
         throw new BadRequestException('There are no fields in the record to create . ');
     }
     if (empty($id)) {
         //Todo:perform logging below
         //Log::error( 'Update request with no id supplied: ' . print_r( $record, true ) );
         throw new BadRequestException('Identifying field "id" can not be empty for update request . ');
     }
     /** @type User $model */
     $model = static::find($id);
     if (!$model instanceof Model) {
         throw new NotFoundException('No resource found for ' . $id);
     }
     $pk = $model->primaryKey;
     //	Remove the PK from the record since this is an update
     ArrayUtils::remove($record, $pk);
     try {
         if ($model->is_sys_admin && !ArrayUtils::getBool($params, 'admin')) {
             throw new ForbiddenException('Not allowed to change an admin user.');
         } elseif (ArrayUtils::getBool($params, 'admin') && !$model->is_sys_admin) {
             throw new BadRequestException('Cannot update a non-admin user.');
         }
         $password = ArrayUtils::get($record, 'password');
         if (!empty($password)) {
             $model->password = $password;
         }
         $model->update($record);
         return static::buildResult($model, $params);
     } catch (\Exception $ex) {
         if (!$ex instanceof ForbiddenException && !$ex instanceof BadRequestException) {
             throw new InternalServerErrorException('Failed to update resource: ' . $ex->getMessage());
         } else {
             throw $ex;
         }
     }
 }
예제 #2
0
 /**
  * @param       $id
  * @param       $record
  * @param array $params
  *
  * @return array
  * @throws \DreamFactory\Core\Exceptions\BadRequestException
  * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException
  * @throws \DreamFactory\Core\Exceptions\NotFoundException
  */
 public static function updateInternal($id, $record, $params = [])
 {
     if (empty($record)) {
         throw new BadRequestException('There are no fields in the record to create . ');
     }
     if (empty($id)) {
         //Todo:perform logging below
         //Log::error( 'Update request with no id supplied: ' . print_r( $record, true ) );
         throw new BadRequestException('Identifying field "id" can not be empty for update request . ');
     }
     $userId = SessionUtility::getCurrentUserId();
     ArrayUtils::set($record, 'user_id', $userId);
     //Making sure name is not changed during update as it not be unique.
     ArrayUtils::set($record, 'name', $id);
     $model = static::whereUserId($userId)->whereName($id)->first();
     if (!$model instanceof Model) {
         throw new NotFoundException('No resource found for ' . $id);
     }
     $pk = $model->primaryKey;
     //	Remove the PK from the record since this is an update
     ArrayUtils::remove($record, $pk);
     try {
         $model->update($record);
         return static::buildResult($model, $params);
     } catch (\Exception $ex) {
         throw new InternalServerErrorException('Failed to update resource: ' . $ex->getMessage());
     }
 }
예제 #3
0
 /**
  * @param       $id
  * @param       $record
  * @param array $params
  *
  * @return array
  * @throws \DreamFactory\Core\Exceptions\BadRequestException
  * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException
  * @throws \DreamFactory\Core\Exceptions\NotFoundException
  */
 public static function updateInternal($id, $record, $params = [])
 {
     if (empty($record)) {
         throw new BadRequestException('There are no fields in the record to create . ');
     }
     if (empty($id)) {
         //Todo:perform logging below
         //Log::error( 'Update request with no id supplied: ' . print_r( $record, true ) );
         throw new BadRequestException('Identifying field "id" can not be empty for update request . ');
     }
     $model = static::find($id);
     if (!$model instanceof Model) {
         throw new NotFoundException('No resource found for ' . $id);
     }
     $pk = $model->primaryKey;
     //	Remove the PK from the record since this is an update
     ArrayUtils::remove($record, $pk);
     try {
         $model->update($record);
         return static::buildResult($model, $params);
     } catch (\Exception $ex) {
         throw new InternalServerErrorException('Failed to update resource: ' . $ex->getMessage());
     }
 }