Passed
Push — master ( 1faf28...95951e )
by Anthony
02:22
created

DroitAcces   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 191
Duplicated Lines 5.76 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 11
loc 191
rs 9.2
c 0
b 0
f 0
wmc 34
lcom 1
cbo 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
	namespace core\admin\droitsacces;
3
4
	use core\App;
5
6
	class DroitAcces {
7
		private $logged;
8
9
		//pour la table identite
10
		private $id_identite;
11
		private $super_admin;
12
13
		//pour la table liste_droit_acces
14
		private $id_liste_droit_acces;
15
16
		//pour des droits pour la gestion des contenus
17
		private $modif_seo;
18
		private $modif_contenu;
19
		private $modif_navigation;
20
		private $supprimer_page;
21
22
23
		//-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------//
24
		public function __construct() {
25
			$dbc = \core\App::getDb();
26
27
			$this->id_identite = $_SESSION["idlogin".CLEF_SITE];
28
29
			//on test voir si super admin
30
			$query = $dbc->select("super_admin")->select("liste_droit")->from("identite")->where("ID_identite", "=", $this->id_identite)->get();
31
32
			if ((is_array($query)) && (count($query) > 0)) {
33
				foreach ($query as $obj) {
34
					$this->super_admin = $obj->super_admin;
35
					$this->id_liste_droit_acces = $obj->liste_droit;
36
				}
37
			}
38
		}
39
		//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------//
40
    
41
    
42
    
43
		//-------------------------- GETTER ----------------------------------------------------------------------------//
44
		public function getLogged() {
45
			return $this->logged;
46
		}
47
		public function getSuperAdmin() {
48
			return $this->super_admin;
49
		}
50
		public function getIdListeDroitAcces() {
51
			return $this->id_liste_droit_acces;
52
		}
53
		public function getModifSeo() {
54
			return $this->modif_seo;
55
		}
56
		public function getModifContenu() {
57
			return $this->modif_contenu;
58
		}
59
		public function getModifNavigation() {
60
			return $this->modif_navigation;
61
		}
62
		public function getSupprimerPage() {
63
			return $this->supprimer_page;
64
		}
65
66
		/**
67
		 * @return array
68
		 */
69
		private function getListeDroitAcces() {
70
			$dbc = App::getDb();
71
72
			$liste_droit_acces = [];
73
74
			$query = $dbc->select()->from("droit_acces")
75
				->from("liste_droit_acces")
76
				->from("liaison_liste_droit")
77
				->where("liste_droit_acces.ID_liste_droit_acces", "=", $this->id_liste_droit_acces, "AND")
78
				->where("droit_acces.ID_droit_acces", "=", "liaison_liste_droit.ID_droit_acces", "AND", true)
79
				->where("liste_droit_acces.ID_liste_droit_acces", "=", "liaison_liste_droit.ID_liste_droit_acces", "", true)
80
				->get();
81
82
			if ((is_array($query)) && (count($query) > 0)) {
83
				foreach ($query as $obj) $liste_droit_acces[] = $obj->droit_acces;
84
			}
85
86
			return $liste_droit_acces;
87
		}
88
89
		/**
90
		 * @return array
91
		 */
92
		private function getListeDroitPage($page) {
93
			$dbc = App::getDb();
94
			$droit_acces = [];
95
96
			$query = $dbc->select("droit_acces")->from("droit_acces")->where("page", " LIKE ", "'%".$page."%'", "", true)->get();
97
			if ((is_array($query)) && (count($query) > 0)) {
98
				foreach ($query as $obj) $droit_acces = $obj->droit_acces;
99
			}
100
101
			return $droit_acces;
102
		}
103
104
		/**
105
		 * @param $id_page
106
		 */
107
		private function getListeDroitModificationContenu($id_page) {
108
			$dbc = App::getDb();
109
110
			//on check si il a le droit de modifier ou supprimer cette page
111
			$query = $dbc->select()->from("droit_acces_page")
112
				->from("liste_droit_acces")
113
				->where("droit_acces_page.ID_page", "=", $id_page, "AND")
114
				->where("liste_droit_acces.ID_liste_droit_acces", "=", $this->id_liste_droit_acces, "AND")
115
				->where("droit_acces_page.ID_liste_droit_acces", "=", "liste_droit_acces.ID_liste_droit_acces", "", true)
116
				->get();
117
118
			//si on a un resultat
119
			if ((is_array($query)) && (count($query) > 0)) {
120
				foreach ($query as $obj) {
121
					$this->modif_seo = $obj->seo;
122
					$this->modif_contenu = $obj->contenu;
123
					$this->modif_navigation = $obj->navigation;
124
					$this->supprimer_page = $obj->supprimer;
125
				}
126
			}
127
		}
128
129
		//autres getter
130
		/**
131
		 * pour savoir si en fonction des droits d'accès de l'utilisateur il peu ou non accéder à cete page
132
		 * on passe outre les test si on est super admin
133
		 * @param string $page
134
		 * @return bool
135
		 */
136
		public function getDroitAccesPage($page) {
137
			//page sans droit dans admin
138
			$all_access = array("configuration/mon-compte", "index", "contacter-support");
139
140
			if (($this->super_admin == 1) || (in_array($this->getListeDroitPage($page), $this->getListeDroitAcces())) || (in_array($page, $all_access))) {
141
				return true;
142
			}
143
			else {
144
				return false;
145
			}
146
		}
147
148
		/**
149
		 * fonction qui permet de gérer les droits d'accès sur les contenus :
150
		 * - creer une page
151
		 * - modifier du contenu (SEO, navigation, contenu)
152
		 * - supprimer une page
153
		 * si on est super admin on passe outre tous les tests
154
		 * @param $droit
155
		 * @param $id_page
156
		 * @return bool|null
157
		 */
158
		public function getDroitAccesContenu($droit, $id_page) {
159
			$liste_droit_acces = $this->getListeDroitAcces();
160
161
			$this->getListeDroitModificationContenu($id_page);
162
163
			$array_modif = [$this->modif_seo, $this->modif_contenu, $this->modif_navigation];
164
165
			//si les trois sont différent de 0 on renvoit true soinon false
166
			if (($this->super_admin == 1) || ((in_array($droit, $liste_droit_acces)) && in_array(1, $array_modif))) {
167
				return true;
168
			}
169
			else {
170
				return false;
171
			}
172
		}
173
174
		/**
175
		 * pour savoir si un utilisateur à le droit de supprimer, modifier ou ajouter des trucs
176
		 * @param $droit_acces
177
		 * @return bool
178
		 */
179
		public function getDroitAccesAction($droit_acces) {
180
			$liste_droit_acces = $this->getListeDroitAcces();
181
182
			if (($this->super_admin == 1) || (in_array($droit_acces, $liste_droit_acces))) {
183
				return true;
184
			}
185
			else {
186
				return false;
187
			}
188
		}
189
		//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
190
    
191
    
192
    
193
		//-------------------------- SETTER ----------------------------------------------------------------------------//
194
195
		//-------------------------- FIN SETTER ----------------------------------------------------------------------------//
196
	}