Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / clipboard / action / UserClipboardAction.class.php
1 <?php
2 namespace wcf\system\clipboard\action;
3 use wcf\data\clipboard\action\ClipboardAction;
4 use wcf\data\user\group\UserGroup;
5 use wcf\system\database\util\PreparedStatementConditionBuilder;
6 use wcf\system\request\LinkHandler;
7 use wcf\system\WCF;
8
9 /**
10 * Prepares clipboard editor items for user objects.
11 *
12 * @author Alexander Ebert
13 * @copyright 2001-2014 WoltLab GmbH
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package com.woltlab.wcf
16 * @subpackage system.clipboard.action
17 * @category Community Framework
18 */
19 class UserClipboardAction extends AbstractClipboardAction {
20 /**
21 * @see \wcf\system\clipboard\action\AbstractClipboardAction::$actionClassActions
22 */
23 protected $actionClassActions = array('delete');
24
25 /**
26 * @see \wcf\system\clipboard\action\AbstractClipboardAction::$supportedActions
27 */
28 protected $supportedActions = array('assignToGroup', 'ban', 'delete', 'exportMailAddress', 'sendMail', 'sendNewPassword');
29
30 /**
31 * @see \wcf\system\clipboard\action\IClipboardAction::execute()
32 */
33 public function execute(array $objects, ClipboardAction $action) {
34 $item = parent::execute($objects, $action);
35
36 if ($item === null) {
37 return null;
38 }
39
40 // handle actions
41 switch ($action->actionName) {
42 case 'assignToGroup':
43 $item->setURL(LinkHandler::getInstance()->getLink('UserAssignToGroup'));
44 break;
45
46 case 'delete':
47 $item->addInternalData('confirmMessage', WCF::getLanguage()->getDynamicVariable('wcf.clipboard.item.com.woltlab.wcf.user.delete.confirmMessage', array(
48 'count' => $item->getCount()
49 )));
50 break;
51
52 case 'exportMailAddress':
53 $item->setURL(LinkHandler::getInstance()->getLink('UserEmailAddressExport'));
54 break;
55
56 case 'sendMail':
57 $item->setURL(LinkHandler::getInstance()->getLink('UserMail'));
58 break;
59
60 case 'sendNewPassword':
61 $item->addParameter('confirmMessage', WCF::getLanguage()->getDynamicVariable('wcf.clipboard.item.com.woltlab.wcf.user.sendNewPassword.confirmMessage', array(
62 'count' => $item->getCount()
63 )));
64 break;
65 }
66
67 return $item;
68 }
69
70 /**
71 * @see \wcf\system\clipboard\action\IClipboardAction::getClassName()
72 */
73 public function getClassName() {
74 return 'wcf\data\user\UserAction';
75 }
76
77 /**
78 * @see \wcf\system\clipboard\action\IClipboardAction::getTypeName()
79 */
80 public function getTypeName() {
81 return 'com.woltlab.wcf.user';
82 }
83
84 /**
85 * Returns the ids of the users which can be deleted.
86 *
87 * @return array<integer>
88 */
89 protected function validateDelete() {
90 // check permissions
91 if (!WCF::getSession()->getPermission('admin.user.canDeleteUser')) {
92 return array();
93 }
94
95 return $this->__validateAccessibleGroups(array_keys($this->objects));
96 }
97
98 /**
99 * Returns the ids of the users which can be banned.
100 *
101 * @return array<integer>
102 */
103 protected function validateBan() {
104 // check permissions
105 if (!WCF::getSession()->getPermission('admin.user.canBanUser')) {
106 return array();
107 }
108
109 return $this->__validateAccessibleGroups(array_keys($this->objects));
110 }
111
112 /**
113 * Validates accessible groups.
114 *
115 * @return array<integer>
116 */
117 protected function __validateAccessibleGroups(array $userIDs, $ignoreOwnUser = true) {
118 if ($ignoreOwnUser) {
119 foreach ($userIDs as $index => $userID) {
120 if ($userID == WCF::getUser()->userID) {
121 unset($userIDs[$index]);
122 }
123 }
124 }
125
126 // no valid users found
127 if (empty($userIDs)) return array();
128
129 // fetch user to group associations
130 $conditions = new PreparedStatementConditionBuilder();
131 $conditions->add("userID IN (?)", array($userIDs));
132
133 $sql = "SELECT userID, groupID
134 FROM wcf".WCF_N."_user_to_group
135 ".$conditions;
136 $statement = WCF::getDB()->prepareStatement($sql);
137 $statement->execute($conditions->getParameters());
138
139 $userToGroup = array();
140 while ($row = $statement->fetchArray()) {
141 if (!isset($userToGroup[$row['userID']])) {
142 $userToGroup[$row['userID']] = array();
143 }
144
145 $userToGroup[$row['userID']][] = $row['groupID'];
146 }
147
148 // validate if user's group is accessible for current user
149 foreach ($userIDs as $userID) {
150 if (!isset($userToGroup[$userID]) || !UserGroup::isAccessibleGroup($userToGroup[$userID])) {
151 unset($userIDs[$userID]);
152 }
153 }
154
155 return $userIDs;
156 }
157
158 /**
159 * Returns the ids of the users which can be sent new passwords.
160 *
161 * @return array<integer>
162 */
163 public function validateSendNewPassword() {
164 // check permissions
165 if (!WCF::getSession()->getPermission('admin.user.canEditPassword')) {
166 return array();
167 }
168
169 return $this->__validateAccessibleGroups(array_keys($this->objects));
170 }
171 }