gitlab_time_tracker-8.x-1.x-dev/modules/gitlab_time_tracker_users/gitlab_time_tracker_users.module
modules/gitlab_time_tracker_users/gitlab_time_tracker_users.module
<?php
/**
* @file
* Contains gitlab_time_tracker_users.module.
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Access\AccessResult;
/**
* Implements hook_help().
*/
function gitlab_time_tracker_users_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the gitlab_time_tracker_users module.
case 'help.page.gitlab_time_tracker_users':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Gitlab as authentication provider') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_node_access_records().
*/
function gitlab_time_tracker_users_node_access_records(NodeInterface $node) {
$grants = [];
if (isset($node->field_gitlab_id) && $node->bundle() == 'project') {
$grants[] = [
'realm' => 'gitlab',
'gid' => $node->field_gitlab_id->value,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
];
}
return $grants;
}
/**
* Implements hook_node_grants().
*/
function gitlab_time_tracker_users_node_grants(AccountInterface $account, $op) {
$session = \Drupal::request()->getSession();
if (is_null($session)) {
return [
'gitlab' => [],
];
}
elseif (!$session->get('gitlab_token')) {
return [
'gitlab' => [],
];
}
elseif ($ids = $session->get('gitlab_grants')) {
return [
'gitlab' => $ids,
];
}
else {
$projects = \Drupal::service('gitlab_time_tracker.gitlab')->fetchProjects();
$ids = array_map(
function ($project) {
return $project['id'];
},
$projects
);
\Drupal::request()->getSession()->set('gitlab_grants', $ids);
return [
'gitlab' => $ids,
];
}
}
