Skip to content

Commit 611002c

Browse files
authored
Merge pull request #69 from sheldor1510/audit-log
Implemented audit logs in mysql
2 parents 0086b73 + fc072d0 commit 611002c

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

resources/lib/UnitySQL.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class UnitySQL
1111
private const TABLE_SSOLOG = "sso_log";
1212
private const TABLE_PAGES = "pages";
1313
private const TABLE_EVENTS = "events";
14+
private const TABLE_AUDIT_LOG = "audit_log";
1415

1516
private const REQUEST_ADMIN = "admin";
1617

@@ -218,4 +219,19 @@ public function addEvent($operator, $action, $entity)
218219

219220
$stmt->execute();
220221
}
222+
223+
// audit log table methods
224+
public function addLog($operator, $operator_ip, $action_type, $recipient)
225+
{
226+
$stmt = $this->conn->prepare(
227+
"INSERT INTO " . self::TABLE_AUDIT_LOG . " (operator, operator_ip, action_type, recipient)
228+
VALUE (:operator, :operator_ip, :action_type, :recipient)"
229+
);
230+
$stmt->bindParam(":operator", $operator);
231+
$stmt->bindParam(":operator_ip", $operator_ip);
232+
$stmt->bindParam(":action_type", $action_type);
233+
$stmt->bindParam(":recipient", $recipient);
234+
235+
$stmt->execute();
236+
}
221237
}

resources/lib/UnityUser.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ public function init($send_mail = true)
112112
// add user to cache
113113
$this->REDIS->appendCacheArray("sorted_users", "", $this->getUID());
114114

115+
//
116+
// add to audit log
117+
//
118+
$this->SQL->addLog(
119+
$this->getUID(),
120+
$_SERVER['REMOTE_ADDR'],
121+
"user_added",
122+
$this->getUID()
123+
);
124+
115125
//
116126
// send email to user
117127
//
@@ -339,9 +349,10 @@ public function getMail($ignorecache = false)
339349
*
340350
* @param array $keys String array of openssh-style ssh public keys
341351
*/
342-
public function setSSHKeys($keys, $send_mail = true)
352+
public function setSSHKeys($keys, $operator = null, $send_mail = true)
343353
{
344354
$ldapUser = $this->getLDAPUser();
355+
$operator = is_null($operator) ? $this->getUID() : $operator->getUID();
345356
$keys_filt = array_values(array_unique($keys));
346357
if ($ldapUser->exists()) {
347358
$ldapUser->setAttribute("sshpublickey", $keys_filt);
@@ -352,6 +363,16 @@ public function setSSHKeys($keys, $send_mail = true)
352363

353364
$this->REDIS->setCache($this->uid, "sshkeys", $keys_filt);
354365

366+
//
367+
// add audit log
368+
//
369+
$this->SQL->addLog(
370+
$operator,
371+
$_SERVER['REMOTE_ADDR'],
372+
"sshkey_modify",
373+
$this->getUID()
374+
);
375+
355376
if ($send_mail) {
356377
$this->MAILER->sendMail(
357378
$this->getMail(),

tools/docker-dev/sql/bootstrap.sql

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ CREATE TABLE `requests` (
9393

9494
-- --------------------------------------------------------
9595

96+
--
97+
-- Table structure for table `audit_log`
98+
--
99+
100+
CREATE TABLE `audit_log` (
101+
`id` int(11) NOT NULL,
102+
`timestamp` timestamp NOT NULL DEFAULT current_timestamp(),
103+
`operator` varchar(1000) NOT NULL,
104+
`operator_ip` varchar(1000) NOT NULL,
105+
`action_type` varchar(1000) NOT NULL,
106+
`recipient` varchar(1000) NOT NULL
107+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
108+
109+
-- --------------------------------------------------------
110+
96111
--
97112
-- Indexes for dumped tables
98113
--
@@ -127,6 +142,12 @@ ALTER TABLE `requests`
127142
ALTER TABLE `sso_log`
128143
ADD PRIMARY KEY (`id`);
129144

145+
--
146+
-- Indexes for table `audit_log`
147+
--
148+
ALTER TABLE `audit_log`
149+
ADD PRIMARY KEY (`id`);
150+
130151
--
131152
-- AUTO_INCREMENT for dumped tables
132153
--
@@ -160,6 +181,12 @@ ALTER TABLE `requests`
160181
--
161182
ALTER TABLE `sso_log`
162183
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
184+
185+
--
186+
-- AUTO_INCREMENT for table `audit_log`
187+
--
188+
ALTER TABLE `audit_log`
189+
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
163190
COMMIT;
164191

165192
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

webroot/panel/account.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@
4646
if (!empty($added_keys)) {
4747
$added_keys = UnitySite::removeTrailingWhitespace($added_keys);
4848
$totalKeys = array_merge($USER->getSSHKeys(), $added_keys);
49-
$USER->setSSHKeys($totalKeys);
49+
$USER->setSSHKeys($totalKeys, $OPERATOR);
5050
}
5151
break;
5252
case "delKey":
5353
$keys = $USER->getSSHKeys();
5454
unset($keys[intval($_POST["delIndex"])]); // remove key from array
5555
$keys = array_values($keys);
5656

57-
$USER->setSSHKeys($keys); // Update user keys
57+
$USER->setSSHKeys($keys, $OPERATOR); // Update user keys
5858
break;
5959
case "loginshell":
6060
if ($_POST["shellSelect"] == "custom") {

0 commit comments

Comments
 (0)