Selaa lähdekoodia

Add redirect count to users list and improve getting a users redirects

ohartl 9 vuotta sitten
vanhempi
commit
93f023d81a

+ 40 - 0
include/php/models/User.php

@@ -25,6 +25,12 @@ class User extends AbstractModel
 	protected $conflictingRedirect = null;
 
 
+	/**
+	 * @var ModelCollection|AbstractRedirect[]
+	 */
+	protected $redirects = null;
+
+
 	/**
 	 * @inheritdoc
 	 */
@@ -247,6 +253,40 @@ class User extends AbstractModel
 	}
 
 
+	/**
+	 * @return ModelCollection|AbstractRedirect[]
+	 */
+	public function getRedirects()
+	{
+		if(is_null($this->redirects)){
+			$this->redirects = AbstractRedirect::findMultiWhere(
+				array(DBC_ALIASES_DESTINATION, 'LIKE', '%'.$this->getEmail().'%')
+			);
+		}
+
+		return $this->redirects;
+	}
+
+
+	/**
+	 * @return ModelCollection|AbstractRedirect[]
+	 */
+	public function getAnonymizedRedirects()
+	{
+		$redirects = $this->getRedirects();
+
+		foreach($redirects as $redirect){
+			$emails = $redirect->getDestination();
+
+			if(is_array($emails) && count($emails) > 1){
+				$redirect->setDestination(array($this->getEmail(), '…'));
+			}
+		}
+
+		return $redirects;
+	}
+
+
 	/**
 	 * Change this users password, throws Exception if password is invalid.
 	 *

+ 12 - 10
include/php/pages/admin/listusers.php

@@ -37,16 +37,17 @@ $users = User::getByLimitedDomains();
 <?php if($users->count() > 0): ?>
 	<table class="table">
 		<thead>
-		<tr>
-			<th>Username</th>
-			<th>Domain</th>
-		<?php if(defined('DBC_USERS_MAILBOXLIMIT')): ?>
-			<th>Mailbox Limit</th>
-		<?php endif; ?>
-			<th>Role</th>
-			<th></th>
-			<th></th>
-		<tr>
+			<tr>
+				<th>Username</th>
+				<th>Domain</th>
+			<?php if(defined('DBC_USERS_MAILBOXLIMIT')): ?>
+				<th>Mailbox Limit</th>
+			<?php endif; ?>
+				<th>Redirect count</th>
+				<th>Role</th>
+				<th></th>
+				<th></th>
+			<tr>
 		</thead>
 		<tbody>
 		<?php foreach($users as $user): /** @var User $user */ ?>
@@ -61,6 +62,7 @@ $users = User::getByLimitedDomains();
 				<?php if(defined('DBC_USERS_MAILBOXLIMIT')): ?>
 					<td style="text-align: right"><?php echo ($user->getMailboxLimit() > 0) ? $user->getMailboxLimit().' MB' : 'No limit'; ?></td>
 				<?php endif; ?>
+				<td><?php echo $user->getRedirects()->count(); ?></td>
 				<td><?php echo ($user->getRole() === User::ROLE_ADMIN) ? 'Admin' : 'User'; ?></td>
 				<td>
 					<a href="<?php echo url('admin/edituser/?id='.$user->getId()); ?>">[Edit]</a>

+ 2 - 13
include/php/pages/private/yourredirects.php

@@ -1,18 +1,7 @@
 <?php
 
-$ownEmail = Auth::getUser()->getEmail();
+$redirects = Auth::getUser()->getAnonymizedRedirects();
 
-$redirects = AbstractRedirect::findMultiWhere(
-	array(DBC_ALIASES_DESTINATION, 'LIKE', '%'.$ownEmail.'%')
-);
-
-function anonymizeEmails($emails, $ownEmail){
-	if(is_string($emails) || count($emails) === 1){
-		return $ownEmail;
-	}
-
-	return array($ownEmail, '&hellip;');
-}
 ?>
 
 	<h1>Redirects to your mailbox</h1>
@@ -35,7 +24,7 @@ function anonymizeEmails($emails, $ownEmail){
 		<?php foreach($redirects as $redirect): /** @var AbstractRedirect $redirect */ ?>
 			<tr>
 				<td><?php echo formatEmails($redirect->getSource(), str_replace(PHP_EOL, '<br>', FRONTEND_EMAIL_SEPARATOR_TEXT)); ?></td>
-				<td><?php echo formatEmails(anonymizeEmails($redirect->getDestination(), $ownEmail), str_replace(PHP_EOL, '<br>', FRONTEND_EMAIL_SEPARATOR_TEXT)); ?></td>
+				<td><?php echo formatEmails($redirect->getDestination(), str_replace(PHP_EOL, '<br>', FRONTEND_EMAIL_SEPARATOR_TEXT)); ?></td>
 			</tr>
 		<?php endforeach; ?>
 		</tbody>