/**
  * {@inheritdoc}
  *
  * When the $operation is 'add' then the $entity is of type 'profile_type',
  * otherwise $entity is of type 'profile'.
  */
 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account)
 {
     $account = $this->prepareUser($account);
     $user_page = \Drupal::request()->attributes->get('user');
     // Some times, operation edit is called update.
     // Use edit in any case.
     if ($operation == 'update') {
         $operation = 'edit';
     }
     // Check that if profile type has require roles, the user the profile is
     // being added to has any of the required roles.
     if ($entity->getEntityTypeId() == 'profile') {
         $profile_roles = ProfileType::load($entity->bundle())->getRoles();
         $user_roles = $entity->getOwner()->getRoles(TRUE);
         if (!empty(array_filter($profile_roles)) && !array_intersect($user_roles, $profile_roles)) {
             return AccessResult::forbidden();
         }
     } elseif ($entity->getEntityTypeId() == 'profile_type') {
         $profile_roles = $entity->getRoles();
         $user_roles = User::load($user_page->id())->getRoles(TRUE);
         if (!empty(array_filter($profile_roles)) && !array_intersect($user_roles, $profile_roles)) {
             return AccessResult::forbidden();
         }
     }
     if ($account->hasPermission('bypass profile access')) {
         return AccessResult::allowed()->cachePerPermissions();
     } elseif ($operation == 'add' && ($user_page->id() == $account->id() && $account->hasPermission($operation . ' own ' . $entity->id() . ' profile') || $account->hasPermission($operation . ' any ' . $entity->id() . ' profile')) || $operation != 'add' && ($entity->getOwnerId() == $account->id() && $account->hasPermission($operation . ' own ' . $entity->getType() . ' profile') || $account->hasPermission($operation . ' any ' . $entity->getType() . ' profile'))) {
         return AccessResult::allowed()->cachePerPermissions();
     } else {
         return AccessResult::forbidden()->cachePerPermissions();
     }
 }
Пример #2
0
 /**
  * Check whether the profile type exists.
  *
  * @param string $id
  *   A string representing a profile type ID.
  *
  * @return bool
  *   Returns bool if profile exists.
  */
 public function exists($id)
 {
     $profile_type = ProfileType::load($id);
     return !empty($profile_type);
 }
Пример #3
0
  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $profile_type = ProfileType::load($this->entity->bundle());

    // Active profile for non administers if profile is new.
    if (!\Drupal::currentUser()->hasPermission('administer profiles') && $this->entity->isNew()) {
      $this->entity->setActive(TRUE);
    }
    switch ($this->entity->save()) {
      case SAVED_NEW:
        drupal_set_message(t('%label profile has been created.', ['%label' => $profile_type->label()]));
        break;
      case SAVED_UPDATED:
        drupal_set_message(t('%label profile has been updated.', ['%label' => $profile_type->label()]));
        break;
    }

    $form_state->setRedirect('entity.user.canonical', [
      'user' => $this->entity->getOwnerId(),
    ]);
  }
Пример #4
0
 /**
  * {@inheritdoc}
  */
 public function label()
 {
     $profile_type = ProfileType::load($this->bundle());
     return t('@type profile of @username (uid: @uid)', ['@type' => $profile_type->label(), '@username' => $this->getOwner()->getDisplayName(), '@uid' => $this->getOwnerId()]);
 }