diff --git a/field_permission_example/css/field_permission_example.css b/field_permission_example/css/field_permission_example.css new file mode 100644 index 0000000..59cda31 --- /dev/null +++ b/field_permission_example/css/field_permission_example.css @@ -0,0 +1,22 @@ +/** + * @file + * CSS for Field Example. + */ +.stickynote { +background:#fefabc; +padding:0.8em; +font-family:cursive; +font-size:1.1em; +color: #000; +width:15em; + +-moz-transform: rotate(2deg); +-webkit-transform: rotate(2deg); +-o-transform: rotate(2deg); +-ms-transform: rotate(2deg); +transform: rotate(2deg); + +box-shadow: 0px 4px 6px #333; +-moz-box-shadow: 0px 4px 6px #333; +-webkit-box-shadow: 0px 4px 6px #333; +} diff --git a/field_permission_example/field_permission_example.info.yml b/field_permission_example/field_permission_example.info.yml new file mode 100644 index 0000000..d59c28f --- /dev/null +++ b/field_permission_example/field_permission_example.info.yml @@ -0,0 +1,8 @@ +name: Field Permission Example +type: module +description: An example module that creates a field and puts access control over it. +package: Example modules +version: 8.x-1.x +core: 8.x +dependencies: + - examples diff --git a/field_permission_example/field_permission_example.libraries.yml b/field_permission_example/field_permission_example.libraries.yml new file mode 100644 index 0000000..ca63cb8 --- /dev/null +++ b/field_permission_example/field_permission_example.libraries.yml @@ -0,0 +1,5 @@ +fieldnote_sticky: + version: 1.x + css: + theme: + css/field_permission_example.css: {} diff --git a/field_permission_example/field_permission_example.links.menu.yml b/field_permission_example/field_permission_example.links.menu.yml new file mode 100644 index 0000000..ce9a56b --- /dev/null +++ b/field_permission_example/field_permission_example.links.menu.yml @@ -0,0 +1,3 @@ +field_permission_example.description: + title: Field Permission Example + route_name: field_permission_example.description diff --git a/field_permission_example/field_permission_example.module b/field_permission_example/field_permission_example.module new file mode 100644 index 0000000..3d14e0f --- /dev/null +++ b/field_permission_example/field_permission_example.module @@ -0,0 +1,166 @@ +getType() != 'field_permission_example_fieldnote') { + return AccessResult::neutral(); + } + + // First we'll check if the user has the 'superuser' + // permissions that node provides. This way administrators + // will be able to administer the content types. + if ($account->hasPermission('bypass node access')) { + drupal_set_message(t('User can bypass node access.')); + return AccessResult::allowed(); + } + if ($account->hasPermission('administer content types', $account)) { + drupal_set_message(t('User can administer content types.')); + return AccessResult::allowed(); + } + if ($account->hasPermission('administer the fieldnote field', $account)) { + drupal_set_message(t('User can administer this field.')); + return AccessResult::allowed(); + } + + //for anyone else, it depends on the desired operation + if ($operation == 'view' and $account->hasPermission('view any fieldnote')) { + drupal_set_message(t('User can view any field note.')); + return AccessResult::allowed(); + } + + if ($operation == 'edit' and $account->hasPermission('edit any fieldnote')) { + drupal_set_message(t('User can edit any field note.')); + return AccessResult::allowed(); + } + + //At this point, we need to know if the user "owns" the entity we're attached to. + //If it's a user, we'll use the account name to test. Otherwise rely on the entity implementing the + //the EntityOwnerInterface. Anything else can't be owned, and we'll refuse access. + if ($items) { + $entity = $items->getEntity(); + if ((($entity instanceof EntityOwnerInterface) and + $entity->getOwner()->getAccountName() == $account->getAccountName()) or + (($entity instanceof UserInterface) and + $entity->name->value == $account->getAccountName()) + ) { + if ($operation == 'view' and $account->hasPermission('view own fieldnote')) { + drupal_set_message(t('User can view their own field note.')); + return AccessResult::allowed(); + } + if ($operation == 'edit' and $account->hasPermission('edit own fieldnote')) { + drupal_set_message(t('User can edit their own field note.')); + return AccessResult::allowed(); + } + } + } + //anything else on this field is forbidden + return AccessResult::forbidden(); +} + +/** + * @} End of "defgroup field_permission_example". + */ diff --git a/field_permission_example/field_permission_example.permissions.yml b/field_permission_example/field_permission_example.permissions.yml new file mode 100644 index 0000000..c70eb2b --- /dev/null +++ b/field_permission_example/field_permission_example.permissions.yml @@ -0,0 +1,11 @@ +# Permissions for the field_permission_example module +'view own fieldnote': + title: View own fieldnote +'edit own fieldnote': + title: Edit own fieldnote +'view any fieldnote': + title: View any fieldnote +'edit any fieldnote': + title: Edit any fieldnote +'administer the fieldnote field': + title: Administer settings for the fieldnote field. diff --git a/field_permission_example/field_permission_example.routing.yml b/field_permission_example/field_permission_example.routing.yml new file mode 100644 index 0000000..aebf40c --- /dev/null +++ b/field_permission_example/field_permission_example.routing.yml @@ -0,0 +1,7 @@ +field_permission_example.description: + path: 'examples/field_permission_example' + defaults: + _title: 'Field Permission Example' + _controller: '\Drupal\field_permission_example\Controller\FieldPermissionExampleController::description' + requirements: + _access: 'TRUE' diff --git a/field_permission_example/src/Controller/FieldPermissionExampleController.php b/field_permission_example/src/Controller/FieldPermissionExampleController.php new file mode 100644 index 0000000..095e96a --- /dev/null +++ b/field_permission_example/src/Controller/FieldPermissionExampleController.php @@ -0,0 +1,34 @@ +l($this->t('the permissions admin page'), $url); + + // Put the link into the content. + $build = array( + '#markup' => $this->t('

The Field Permission Example provides a simple "fieldnote" widget that can be attached to any fieldable entity. View and Edit access to the field is controlled via the permissions for this module, which you can assign to roles and users, which you can enable at the !permissions_admin_link.

', ['!permissions_admin_link' => $permissions_admin_link]), + ); + + return $build; + } + +} diff --git a/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php b/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php new file mode 100644 index 0000000..ac9229c --- /dev/null +++ b/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php @@ -0,0 +1,54 @@ + $item) { + $elements[$delta] = array( + // We wrap the fieldnote content up in a div tag. + '#type' => 'html_tag', + '#tag' => 'div', + //this text is auto-XSS escaped. See docs for @RenderElement("html_tag") + '#value' => $item->value, + // Let's give the note a nice sticky-note CSS appearance. + '#attributes' => array( + 'class' => 'stickynote', + ), + // ..And this is the CSS for the stickynote. + '#attached' => array( + 'library' => array('field_permission_example/fieldnote_sticky'), + ), + ); + } + + return $elements; + } + +} diff --git a/field_permission_example/src/Plugin/Field/FieldType/FieldNote.php b/field_permission_example/src/Plugin/Field/FieldType/FieldNote.php new file mode 100644 index 0000000..bdccde4 --- /dev/null +++ b/field_permission_example/src/Plugin/Field/FieldType/FieldNote.php @@ -0,0 +1,60 @@ + array( + 'value' => array( + 'type' => 'text', + 'size' => 'normal', + 'not null' => FALSE, + ), + ), + ); + } + + /** + * {@inheritdoc} + */ + public function isEmpty() { + $value = $this->get('value')->getValue(); + return $value === NULL || $value === ''; + } + + /** + * {@inheritdoc} + */ + public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { + $properties['value'] = DataDefinition::create('string') + ->setLabel(t('Field Note')); + + return $properties; + } +} diff --git a/field_permission_example/src/Plugin/Field/FieldWidget/TextWidget.php b/field_permission_example/src/Plugin/Field/FieldWidget/TextWidget.php new file mode 100644 index 0000000..1cf08db --- /dev/null +++ b/field_permission_example/src/Plugin/Field/FieldWidget/TextWidget.php @@ -0,0 +1,40 @@ +value) ? $items[$delta]->value : ''; + $element += array( + '#type' => 'textarea', + '#default_value' => $value, + ); + return array('value' => $element); + } + +}