GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

GitPermissionsManager::updateSiteAccess()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 8.8571
cc 5
eloc 9
nc 9
nop 2
1
<?php
2
/**
3
 * Copyright (c) Enalean, 2014. All Rights Reserved.
4
 *
5
 * This file is a part of Tuleap.
6
 *
7
 * Tuleap is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * Tuleap is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with Tuleap; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21
22
require_once 'www/project/admin/permissions.php';
23
24
/**
25
 * This class manages permissions for the Git service
26
 */
27
class GitPermissionsManager {
28
29
    /**
30
     * @var Git_SystemEventManager
31
     */
32
    private $git_system_event_manager;
33
34
    /**
35
     * @var Git_PermissionsDao
36
     */
37
    private $git_permission_dao;
38
39
    /**
40
     * @var PermissionsManager
41
     */
42
    private $permissions_manager;
43
44
    public function __construct(Git_PermissionsDao $git_permission_dao, Git_SystemEventManager $git_system_event_manager) {
45
        $this->permissions_manager      = PermissionsManager::instance();
46
        $this->git_permission_dao       = $git_permission_dao;
47
        $this->git_system_event_manager = $git_system_event_manager;
48
    }
49
50
    public function userIsGitAdmin(PFUser $user, Project $project) {
51
        $database_result = $this->getCurrentGitAdminPermissionsForProject($project);
52
53
        if (db_numrows($database_result) < 1) {
54
            $database_result = $this->getDefaultGitAdminPermissions();
55
        }
56
57
        $has_permission = false;
58
        while (! $has_permission && ($row = db_fetch_array($database_result))) {
59
            $has_permission = ugroup_user_is_member($user->getId(), $row['ugroup_id'], $project->getID());
60
        }
61
62
        return $has_permission;
63
    }
64
65
    /**
66
     * @param Project $project
67
     * Return a DB list of ugroup_ids authorized to access the given object
68
     */
69
    private function getCurrentGitAdminPermissionsForProject(Project $project) {
70
        return permission_db_authorized_ugroups(Git::PERM_ADMIN, $project->getID());
71
    }
72
73
    private function getDefaultGitAdminPermissions() {
74
        return permission_db_get_defaults(Git::PERM_ADMIN);
0 ignored issues
show
Deprecated Code introduced by
The function permission_db_get_defaults() has been deprecated.

This function has been deprecated.

Loading history...
75
    }
76
77
    public function getCurrentGitAdminUgroups($project_id) {
78
        return $this->permissions_manager->getAuthorizedUgroupIds($project_id, Git::PERM_ADMIN);
79
    }
80
81
    public function updateAccessForRepositories(Project $project, $old_access, $new_access) {
82
        if ($new_access == Project::ACCESS_PRIVATE) {
83
            $this->git_permission_dao->disableAnonymousRegisteredAuthenticated($project->getID());
84
            $this->git_system_event_manager->queueProjectsConfigurationUpdate(array($project->getID()));
85
        }
86
        if ($new_access == Project::ACCESS_PUBLIC && $old_access == Project::ACCESS_PUBLIC_UNRESTRICTED) {
87
            $this->git_permission_dao->disableAuthenticated($project->getID());
88
            $this->git_system_event_manager->queueProjectsConfigurationUpdate(array($project->getID()));
89
        }
90
    }
91
92
    public function updateSiteAccess($old_value, $new_value) {
93
        if ($old_value == ForgeAccess::ANONYMOUS) {
94
            $project_ids = $this->queueProjectsConfigurationUpdate($this->git_permission_dao->getAllProjectsWithAnonymousRepositories());
95
            if (count($project_ids)) {
96
                $this->git_permission_dao->updateAllAnonymousRepositoriesToRegistered();
97
            }
98
        }
99
        if ($old_value == ForgeAccess::RESTRICTED) {
100
            $project_ids = $this->queueProjectsConfigurationUpdate($this->git_permission_dao->getAllProjectsWithUnrestrictedRepositories());
101
            if (count($project_ids)) {
102
                $this->git_permission_dao->updateAllAuthenticatedRepositoriesToRegistered();
103
            }
104
        }
105
    }
106
107
    private function queueProjectsConfigurationUpdate(DataAccessResult $dar) {
108
        $projects_ids = array();
109
        if (count($dar) > 0) {
110
            foreach ($dar as $row) {
111
                $projects_ids[] = $row['group_id'];
112
            }
113
            $this->git_system_event_manager->queueProjectsConfigurationUpdate($projects_ids);
114
        }
115
        return $projects_ids;
116
    }
117
}
118