FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UserHelper.php
1 <?php
2 
14 namespace Fossology\UI\Api\Helper;
15 
21 use Symfony\Component\HttpFoundation\Request;
22 
28 {
32  private $user_pk;
33 
39  public function __construct($user_pk=null)
40  {
41  $this->user_pk = $user_pk;
42  }
43 
44  public function modifyUserDetails($reqBody, $version = ApiVersion::V1)
45  {
46  global $container;
47  $restHelper = $container->get('helper.restHelper');
48  $userEditObj = $restHelper->getPlugin('user_edit');
49  /* Is the session owner an admin? */
50  $sessionOwnerUser_pk = $restHelper->getUserId();
51  $SessionUserRec = $userEditObj->GetUserRec($sessionOwnerUser_pk);
52  $SessionIsAdmin = $userEditObj->IsSessionAdmin($SessionUserRec);
53 
54  $symReq = $this->createSymRequest($reqBody, $version);
55  if (!$SessionIsAdmin) {
56  $returnVal = new Info(403, "The session owner is not an admin!", InfoType::INFO);
57  } else {
58  $userRec = $userEditObj->CreateUserRec($symReq);
59  $ErrMsgs = $userEditObj->UpdateUser($userRec, $SessionIsAdmin);
60 
61  if ($ErrMsgs == null) {
62  $returnVal = new Info(200, "User updated succesfully!", InfoType::INFO);
63  } else {
64  $returnVal = new Info(400, $ErrMsgs, InfoType::INFO);
65  }
66  }
67  return $returnVal;
68  }
69 
74  public function createSymRequest($userDetails, $version = ApiVersion::V1)
75  {
76  global $container;
77  $restHelper = $container->get('helper.restHelper');
78 
83  $userDao = $restHelper->getUserDao();
84  $user = $userDao->getUserByPk($this->user_pk);
85 
86  $symfonyRequest = new Request();
87  $symfonyRequest->request->set('user_pk', $userDetails['id'] ?? $this->user_pk);
88  $symfonyRequest->request->set('user_name', $userDetails['name'] ?? $user['user_name']);
89  $symfonyRequest->request->set('root_folder_fk', $userDetails['rootFolderId'] ?? $user['root_folder_fk']);
90  $symfonyRequest->request->set('default_group_fk', $userDetails['defaultGroup'] ?? $user['group_fk']);
91  $symfonyRequest->request->set('public', $userDetails['defaultVisibility'] ?? $user['upload_visibility']);
92  $symfonyRequest->request->set('default_folder_fk', $userDetails['defaultFolderId'] ?? $user['default_folder_fk']);
93  $symfonyRequest->request->set('user_desc', $userDetails['description'] ?? $user['user_desc']);
94  $symfonyRequest->request->set('_pass1', $userDetails[$version == ApiVersion::V2 ? 'userPass' : 'user_pass'] ?? null);
95  $symfonyRequest->request->set('_pass2', $userDetails[$version == ApiVersion::V2 ? 'userPass' : 'user_pass'] ?? null);
96  $symfonyRequest->request->set('_blank_pass', $userDetails['_blank_pass'] ?? "");
97  $symfonyRequest->request->set('user_status', $userDetails['user_status'] ?? $user['user_status']);
98  $symfonyRequest->request->set('user_email', $userDetails['email'] ?? $user['user_email']);
99  $symfonyRequest->request->set('email_notify', isset($userDetails['emailNotification']) && $userDetails['emailNotification'] ? "y" : $user['email_notify']);
100  $symfonyRequest->request->set('default_bucketpool_fk', $userDetails['defaultBucketpool'] ?? $user['default_bucketpool_fk']);
101 
102  if (isset($userDetails['accessLevel'])) {
103  $user_perm = $this->getEquivalentValueForPermission($userDetails['accessLevel']);
104  $symfonyRequest->request->set('user_perm', $user_perm);
105  } else {
106  $symfonyRequest->request->set('user_perm', $user['user_perm']);
107  }
108 
109  $agentsExists = array();
110  // setting previous values from db
111  $agentsTempVal = explode(',', $user['user_agent_list']);
112  foreach ($agentsTempVal as $agent) {
113  $agentsExists['Check_' . $agent] = 1;
114  }
115  $newAgents = array();
116  if (isset($userDetails['agents'])) {
117  if (is_string($userDetails['agents'])) {
118  $userDetails['agents'] = json_decode($userDetails['agents'], true);
119  }
120  if (isset($userDetails['agents']['mime'])) {
121  $newAgents['Check_agent_mimetype'] = $userDetails['agents']['mime'] ? 1 : 0;
122  }
123  if (isset($userDetails['agents']['monk'])) {
124  $newAgents['Check_agent_monk'] = $userDetails['agents']['monk'] ? 1 : 0;
125  }
126  if (isset($userDetails['agents']['ojo'])) {
127  $newAgents['Check_agent_ojo'] = $userDetails['agents']['ojo'] ? 1 : 0;
128  }
129  if (isset($userDetails['agents'][$version == ApiVersion::V2 ? 'copyrightEmailAuthor' : 'copyright_email_author'])) {
130  $newAgents['Check_agent_copyright'] = $userDetails['agents'][$version == ApiVersion::V2 ? 'copyrightEmailAuthor' : 'copyright_email_author'] ? 1 : 0;
131  }
132  if (isset($userDetails['agents']['ecc'])) {
133  $newAgents['Check_agent_ecc'] = $userDetails['agents']['ecc'] ? 1 : 0;
134  }
135  if (isset($userDetails['agents']['keyword'])) {
136  $newAgents['Check_agent_keyword'] = $userDetails['agents']['keyword'] ? 1 : 0;
137  }
138  if (isset($userDetails['agents']['nomos'])) {
139  $newAgents['Check_agent_nomos'] = $userDetails['agents']['nomos'] ? 1 : 0;
140  }
141  if (isset($userDetails['agents']['package'])) {
142  $newAgents['Check_agent_pkgagent'] = $userDetails['agents']['package'] ? 1 : 0;
143  }
144  if (isset($userDetails['agents']['reso'])) {
145  $newAgents['Check_agent_reso'] = $userDetails['agents']['reso'] ? 1 : 0;
146  }
147  if (isset($userDetails['agents']['heritage'])) {
148  $newAgents['Check_agent_shagent'] = $userDetails['agents']['heritage'] ? 1 : 0;
149  }
150  // Make sure all agents are in the list
151  $agentList = listAgents();
152  foreach (array_keys($agentList) as $agentName) {
153  if (!array_key_exists("Check_$agentName", $newAgents)) {
154  $newAgents["Check_$agentName"] = 0;
155  }
156  }
157  }
158  $agents = array_replace($agentsExists, $newAgents);
159 
160  $symfonyRequest->request->set('user_agent_list', userAgents($agents));
161 
162  return $symfonyRequest;
163  }
164 
169  public function getEquivalentValueForPermission($perm)
170  {
171  switch ($perm) {
172  case 'read_only':
173  return Auth::PERM_READ;
174  case 'read_write':
175  return Auth::PERM_WRITE;
176  case 'clearing_admin':
177  return Auth::PERM_CADMIN;
178  case 'admin':
179  return Auth::PERM_ADMIN;
180  default:
181  return Auth::PERM_NONE;
182  }
183  }
184 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Handle user related queries.
Definition: UserHelper.php:28
Different type of infos provided by REST.
Definition: InfoType.php:16
Info model to contain general error and return values.
Definition: Info.php:19
userAgents($agents=null)
Read the UI form and format the user selected agents into a comma separated list.
listAgents()
Search in available plugins and return all agents.
REST api helper classes.