Skip to content
Snippets Groups Projects
SecondaryMailController.php 3.24 KiB
<?php
/**
 * @copyright Copyright (c) 2019 Reinhold Kainhofer <office@open-tools.net>
 *
 * @author Reinhold Kainhofer <office@open-tools.net>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OCA\SecondaryMail\Controller;

use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\IConfig;
use OCP\IUserManager;
use OCP\IUserSession;

class SecondaryMailController extends OCSController {
	private $config;
	private $userManager;
	protected $user;

	public function __construct(
		$appName,
		IRequest $request,
		IConfig $config,
		IUserManager $userManager,
		IUserSession $userSession
	) {
		parent::__construct($appName, $request);
		$this->config = $config;
		$this->userManager = $userManager;
		$this->user = $userSession->getUser()->getUID();
	}

	/**
	 * @NoAdminRequired
	 * @PasswordConfirmationRequired
	 */
	public function setProperty($key, $value) {
		return $this->setPropertyForUser($this->user, $key, $value);
	}

	/**
	 * @NoAdminRequired
	 * @PasswordConfirmationRequired
	 */
	public function setPropertyForUser($id, $key, $value) {
		$data = array();
		$data['user'] = $id;
		$data['key'] = $key;
		$data['value'] = $value;
		if (in_array($key, array('emailUsePrimary', 'emailUseSecondary', 'email2'))) {
			if ($key === 'email2' && $value === "") {
				$this->config->deleteUserValue($id, 'settings', $key);
				$data['action'] = "deleted";
			} else {
				$this->config->setUserValue($id, 'settings', $key, $value);
				$data['action'] = "changed";
			}
			$data['success'] = true;
			return new DataResponse($data);
		} else {
			$data['action'] = "UNKNOWN KEY";
			$data['success'] = false;
			return new DataResponse($data);
		}
	}

	/**
	 * @NoAdminRequired
	 * @PasswordConfirmationRequired
	 */
	public function getAddress($id) {
		$mails = array();
		$data = array();
		
		$usePrimary = $this->config->getUserValue($id, 'settings', 'emailUsePrimary', 'true');
		$data['usePrimary'] = $usePrimary;
		if ($usePrimary === 'true' || $usePrimary === 1 || $usePrimary === "Y") {
			$user = $this->userManager->get($id);
			$email = $user->getEMailAddress();
			if (!empty($email)) {
				$mails[] = $user->getEMailAddress();
			}
		}
		
		$useSecondary = $this->config->getUserValue($id, 'settings', 'emailUseSecondary', 'true');
		$data['useSecondary'] = $useSecondary;
		if ($useSecondary === 'true' || $useSecondary === 1 || $useSecondary === "Y") {
			$secondary = $this->config->getUserValue($id, 'settings', 'email2');
			if (!empty($secondary)) {
				$mails[] = $secondary;
			}
		}
		$data['email'] = $mails;
		return new DataResponse($data);
	}

}