<?php
namespace EADPlataforma\Controller\Admin;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use EADPlataforma\Entity\User;
use EADPlataforma\Entity\UserProfile;
use EADPlataforma\Entity\Country;
use EADPlataforma\Entity\State;
use EADPlataforma\Entity\City;
use EADPlataforma\Entity\UserCustomField;
use EADPlataforma\Entity\UserCheckoutInfo;
use EADPlataforma\Entity\Group;
use EADPlataforma\Entity\Session;
use EADPlataforma\Entity\Receiver;
use EADPlataforma\Entity\Enrollment;
use EADPlataforma\Entity\Cart;
use EADPlataforma\Entity\Configuration;
use EADPlataforma\DataTransferObject\UserDTO;
use EADPlataforma\Enum\UserEnum;
use EADPlataforma\Enum\ConfigurationEnum;
use EADPlataforma\Enum\SessionEnum;
use EADPlataforma\Enum\ReceiverEnum;
use EADPlataforma\Enum\UserCustomFieldEnum;
use EADPlataforma\Enum\UserPermissionEnum;
use EADPlataforma\Enum\UserCheckoutInfoEnum;
use EADPlataforma\Enum\TagsMarketingEnum;
use EADPlataforma\Enum\ErrorEnum;
use EADPlataforma\Util\StringUtil;
/**
* @Route(
* schemes = {"http|https"}
* )
* @Cache(
* maxage = "0",
* smaxage = "0",
* expires = "now",
* public = false
* )
*/
class UserController extends AbstractController {
public function getEntityClass(){
return User::class;
}
/**
* @Route(
* path = "/admin/user/list/paginate",
* name = "userPaginate",
* methods = {"GET"},
* )
*/
public function getUserPaginate(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "see");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$columns = [
"u.id",
"u.name",
"u.email",
"up.name AS profile",
"u.type",
"DATE_FORMAT(u.dateRegister, '%Y-%m-%d %H:%i:%s') AS dateRegister",
"DATE_FORMAT(u.dateLastAccess, '%Y-%m-%d %H:%i:%s') AS dateLastAccess",
"u.status",
"u.validEmail",
"u.document",
"u.photo",
"u.occupation",
"u.biography",
"u.resume",
"u.custom",
"u.notes",
"DATE_FORMAT(u.birthDate, '%Y-%m-%d') AS birthDate",
"u.phone",
"u.zipCode",
"u.address",
"u.addressNumber",
"u.addressComplement",
"u.addressNeighborhood",
"u.customField",
"DATE_FORMAT(u.dateDelete, '%Y-%m-%d %H:%i:%s') AS dateDelete",
"ud.name AS userDelete"
];
$profileClass = UserProfile::class;
$userClass = User::class;
$enrollmentClass = Enrollment::class;
$joins = [
"{$profileClass} AS up" => "up.id = u.userProfile",
"{$userClass} AS ud" => ["LEFT", "ud.id = u.userDelete"]
];
$type = $this->requestUtil->getField('type');
$status = $this->requestUtil->getField('status');
$userProfile = $this->requestUtil->getField('userProfile');
$validEmail = $this->requestUtil->getField('validEmail');
$searchText = $this->requestUtil->getField('searchText');
$orderParam = $this->requestUtil->getField('order');
$limit = $this->requestUtil->getField('limit');
$offset = $this->requestUtil->getField('offset');
$dateStart = $this->requestUtil->getField('dateStart');
$dateEnd = $this->requestUtil->getField('dateEnd');
$recover = (int)$this->requestUtil->getField('recover');
$course = (int)$this->requestUtil->getField('course');
$filter = [];
$filterDelete = $this->requestUtil->getDeletedParam();
$filter = [
"u.id" => [ "!=", 1 ]
];
if(!empty($type)){
$filter["u.type"] = $type;
}
if(!is_null($status)){
$filter["u.status"] = $status;
}
if(!empty($course)){
$filter["whereText"] = " u.id IN (
SELECT
IDENTITY(e.user)
FROM {$enrollmentClass} AS e
WHERE e.deleted = 0
AND e.course = {$course}
) ";
}
if($recover == UserEnum::YES){
$today = date('Y-m-d');
$filter["u.status"] = UserEnum::WAITING;
$filter["whereText"] = "
(DATE_FORMAT(u.recoverDate, '%Y-%m-%d') != '{$today}' OR u.recoverDate IS NULL)
";
}
if(!empty($userProfile)){
$filter["u.userProfile"] = $userProfile;
}
if(!is_null($validEmail)){
$filter["u.validEmail"] = $validEmail;
}
if(!empty($dateStart) && !empty($dateEnd)){
$filter["whereText"] = "
DATE_FORMAT(u.dateRegister, '%Y-%m-%d') BETWEEN '{$dateStart}' AND '{$dateEnd}'
";
}
$order = [ "u.id" => "DESC" ];
if(!empty($orderParam)){
$orderJson = json_decode($orderParam, true);
$order = (!empty($orderJson) && is_array($orderJson) ? $orderJson : $order);
}
$data = $this->repository->paginate(
"u",
$searchText,
$columns,
$joins,
$filter,
$order,
$limit,
$offset,
$filterDelete
);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/list/paginate/all",
* name = "userPaginateAll",
* methods = {"GET"},
* )
*/
public function getUserPaginateAll(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$columns = [
"u.id",
"u.name",
"u.email",
"up.name AS profile",
"u.type",
"DATE_FORMAT(u.dateRegister, '%Y-%m-%d %H:%i:%s') AS dateRegister",
"DATE_FORMAT(u.dateLastAccess, '%Y-%m-%d %H:%i:%s') AS dateLastAccess",
"u.status",
"u.validEmail",
"u.document",
"u.photo",
"u.occupation",
"u.biography",
"u.resume",
"u.custom",
"u.notes",
"DATE_FORMAT(u.birthDate, '%Y-%m-%d') AS birthDate",
"u.phone",
"u.zipCode",
"u.address",
"u.addressNumber",
"u.addressComplement",
"u.addressNeighborhood",
"u.customField",
"DATE_FORMAT(u.dateDelete, '%Y-%m-%d %H:%i:%s') AS dateDelete",
"ud.name AS userDelete"
];
$profileClass = UserProfile::class;
$userClass = User::class;
$enrollmentClass = Enrollment::class;
$joins = [
"{$profileClass} AS up" => "up.id = u.userProfile",
"{$userClass} AS ud" => ["LEFT", "ud.id = u.userDelete"]
];
$type = (int)$this->requestUtil->getField('type');
$status = $this->requestUtil->getField('status');
$userProfile = (int)$this->requestUtil->getField('userProfile');
$enrollment = (int)$this->requestUtil->getField('enrollment');
$validEmail = $this->requestUtil->getField('validEmail');
$orderParam = $this->requestUtil->getField('order');
$searchText = $this->requestUtil->getField('searchText');
$limit = (int)$this->requestUtil->getField('limit');
$offset = (int)$this->requestUtil->getField('offset');
$dateStart = $this->requestUtil->getField('dateStart');
$dateEnd = $this->requestUtil->getField('dateEnd');
$filter = [];
$filterDelete = $this->requestUtil->getDeletedParam();
$filter = [
"u.id" => [ "!=", 1 ],
];
if($enrollment == UserEnum::YES){
$filter["whereText"] = " u.id IN (
SELECT
IDENTITY(e.user)
FROM {$enrollmentClass} AS e
WHERE e.deleted = 0
AND e.user != 1
) ";
}
if(!empty($type)){
$filter["u.type"] = $type;
}
if(!is_null($status)){
$filter["u.status"] = $status;
}
if(!empty($userProfile)){
$filter["u.userProfile"] = $userProfile;
}
if(!is_null($validEmail)){
$filter["u.validEmail"] = $validEmail;
}
if(!empty($dateStart) && !empty($dateEnd)){
$filter["whereText"] = "DATE_FORMAT(u.dateRegister, '%Y-%m-%d') BETWEEN '{$dateStart}' AND '{$dateEnd}'";
}
$order = [ "u.id" => "DESC" ];
if(!empty($orderParam)){
$order = json_decode($orderParam, true);
}
$data = $this->repository->paginate(
"u",
$searchText,
$columns,
$joins,
$filter,
$order,
$limit,
$offset,
$filterDelete
);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/list/all/trash",
* name = "userAllTrash",
* methods = {"GET"},
* )
*/
public function getUserAllTrash(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$data = $this->repository->getAllTrash();
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/new/number",
* name = "userNewNumber",
* methods = {"GET"},
* )
*/
public function getUserNewNumber(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "see");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$period = $request->get('period');
$dateStart = $request->get('dateStart');
$dateEnd = $request->get('dateEnd');
if($period == UserEnum::FILTER_PERIOD_All){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
}
$dateTimeUtil = $this->generalService->getUtil('DateTimeUtil');
$dates = $dateTimeUtil->periodDate($period, $dateStart, $dateEnd);
$dateStart = $dates->dateStart;
$dateEnd = $dates->dateEnd;
$numberTotal = $this->repository->getNewUsersNumber($dateStart, $dateEnd);
return $this->eadResponse([ "total" => $numberTotal ]);
}
/**
* @Route(
* path = "/admin/user/active/number",
* name = "userActiveNumber",
* methods = {"GET"},
* )
*/
public function getActiveUsersNumber(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "see");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$period = $request->get('period');
$dateStart = $request->get('dateStart');
$dateEnd = $request->get('dateEnd');
if($period == UserEnum::FILTER_PERIOD_All){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
}
$dateTimeUtil = $this->generalService->getUtil('DateTimeUtil');
$dates = $dateTimeUtil->periodDate($period, $dateStart, $dateEnd);
$dateStart = $dates->dateStart;
$dateEnd = $dates->dateEnd;
$numberTotal = $this->repository->getActiveUsersNumber($dateStart, $dateEnd);
return $this->eadResponse([ "total" => $numberTotal ]);
}
/**
* @Route(
* path = "/admin/user/list/admin",
* name = "userListAdmin",
* methods = {"GET"},
* )
*/
public function getUserAdmin(Request $request) {
$product = (int)$request->get('product');
$course = (int)$request->get('course');
$data = $this->repository->getUserAdminSimply($product, $course);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/list/receiver",
* name = "getUserReceiverList",
* methods = {"GET"}
* )
*/
public function getUserReceiverList(Request $request) {
$permission = $this->userPermissionUtil->getPermission(
"financial",
"receiver",
"create"
);
$data = [];
if($this->userPermissionUtil->isLow($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}else if(
$this->userPermissionUtil->isMiddle($permission) &&
$this->user->getAllowReceiver() == UserEnum::YES
){
$data[] = (object)[
"id" => $this->user->getId(),
"name" => $this->user->getName(),
"email" => $this->user->getEmail(),
];
}else if($this->userPermissionUtil->isHigh($permission)){
$data = $this->repository->getUserAllowReceiver();
}
$accountType = ReceiverEnum::EAD_CHECKOUT;
$receiverRepository = $this->em->getRepository(Receiver::class);
$aux = [];
foreach ($data as $key => $item) {
$item = (object)$item;
$numberReceiver = $receiverRepository->countByType(
$accountType,
$item->id
);
if($numberReceiver < 2){
$aux[] = $item;
}
}
return $this->eadResponse($aux);
}
/**
* @Route(
* path = "/admin/user/list/manager",
* name = "userListCourseManager",
* methods = {"GET"},
* )
*/
public function getUserCourseManager(Request $request) {
$product = (int)$request->get('product');
$course = (int)$request->get('course');
$data = $this->repository->getUserCourseManager($product, $course);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/list/group/manager",
* name = "userListGroupManager",
* methods = {"GET"},
* )
*/
public function userListGroupManager(Request $request) {
$data = $this->repository->getUserAdminToGroupPermission();
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/profile/{username}",
* name = "userProfile",
* methods = {"GET"},
* )
*/
public function getUserProfileByUsername(Request $request) {
$username = $request->get('username');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"username" => $username,
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
return $this->eadResponse($user->toReturn(true));
}
/**
* @Route(
* path = "/admin/user/profile",
* name = "userProfileOn",
* methods = {"GET"},
* )
*/
public function getUserProfile(Request $request) {
$user = $this->user;
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
return $this->eadResponse($user->toReturn(true));
}
/**
* @Route(
* path = "/admin/user/permissions",
* name = "userPermissions",
* methods = {"GET"},
* )
*/
public function getUserPermissions(Request $request) {
if(!$this->user){
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$permissions = json_decode($this->user->getPermission());
if($this->user->getId() == 1){
$permissions = $this->userPermissionUtil->getAdminPermissionTemplate();
}
$permissions->menu = (object)[];
$permissions->menu->dashboardModule = $this->userPermissionUtil->canAccessDashboardModule();
$permissions->menu->productModule = $this->userPermissionUtil->canAccessProductModule();
$permissions->menu->courseModule = $this->userPermissionUtil->canAccessCourseModule();
$permissions->menu->userModule = $this->userPermissionUtil->canAccessUserModule();
$permissions->menu->financialModule = $this->userPermissionUtil->canAccessFinancialModule();
$permissions->menu->websiteModule = $this->userPermissionUtil->canAccessWebsiteModule();
$permissions->menu->appStoreModule = $this->userPermissionUtil->canAccessAppStore();
$permissions->menu->trashModule = $this->userPermissionUtil->canAccessTrashModule();
$permissions->menu->planModule = $this->userPermissionUtil->canAccessPlanModule();
return $this->eadResponse($permissions, null, false);
}
/**
* @Route(
* path = "/admin/user/permissions/front/clean",
* name = "userPermissionsFrontClean",
* methods = {"GET"},
* )
*/
public function getUserPermissionsFrontClean(Request $request) {
if(!$this->configuration->checkModuleIsAbleOnPlan('permissionFunction')){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
}
if(!$this->user){
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$data = $this->userPermissionUtil->getPermissionToFrontClean();
return $this->eadResponse($data, null, false);
}
/**
* @Route(
* path = "/admin/user/permissions/front/template",
* name = "userPermissionsFrontTemplate",
* methods = {"GET"},
* )
*/
public function getUserPermissionsFrontTemplate(Request $request) {
if(!$this->configuration->checkModuleIsAbleOnPlan('permissionFunction')){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
}
if(!$this->user){
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$data = $this->userPermissionUtil->getStudentPermissionTemplate();
return $this->eadResponse($data, null, false);
}
/**
* @Route(
* path = "/admin/user/detail/{id}",
* name = "userDetail",
* methods = {"GET"},
* requirements = { "id" = "\d+" }
* )
*/
public function getUserDetail(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "see");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$ignoreOption = $this->requestUtil->getField('ignoreOption');
$userId = $request->get('id');
if($ignoreOption == UserEnum::ITEM_ON_TRASH){
$user = $this->repository->findOneBy([
"id" => $userId
]);
}
if($ignoreOption == UserEnum::ITEM_NO_DELETED || !$ignoreOption){
$user = $this->repository->findOneBy([
"id" => $userId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
}
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($user->getId() == UserEnum::FIRST_ADMIN && $user->getType() == UserEnum::ADMIN){
$user->setPermission(
json_encode($this->userPermissionUtil->getAdminPermissionTemplate())
);
$this->em->flush();
}
$data = $user->toReturn();
$data['urlProfile'] = $this->generalService->generateUrl(
'userProfilePublic',
[ 'username' => $user->getUsername() ]
);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/user/download/custom/file/{id}/{fieldId}",
* name = "userDownloadCustom",
* methods = {"GET"},
* requirements = { "id" = "\d+", "fieldId" = "\d+" }
* )
*/
public function downloadCustomFieldFile(Request $request) {
if(!$this->configuration->checkModuleIsAbleOnPlan('customFormModule')){
return $this->redirectToRoute('notFound');
}
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->redirectToRoute('notFound');
}
if($userId != $this->user->getId()){
$permission = $this->userPermissionUtil->getPermission("user", "see");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->redirectToRoute('notFound');
}
}
$fieldId = $request->get('fieldId');
$fields = json_decode($user->getCustomField());
$url = null;
if(!empty($fields->{$fieldId})){
if(!empty($fields->{$fieldId}->value)){
$file = $fields->{$fieldId}->value->path;
$othersPath = UserEnum::PATH_OTHERS_PROFILES;
$this->fileService->setFile("{$othersPath}/{$file}");
$url = $this->fileService->getFileUrlTemp();
}
}
if($url){
return $this->redirect($url, 301);
}
return $this->redirectToRoute('notFound');
}
/**
* @Route(
* path = "/admin/user/merge",
* name = "userMerge",
* methods = {"POST"},
* )
*/
public function mergeUser(Request $request) {
if($this->user->getId() != UserEnum::YES){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$userIdOld = $this->requestUtil->getField('userOld');
$userIdNew = $this->requestUtil->getField('userNew');
$userOld = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userIdOld
]);
$userNew = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userIdNew
]);
if (!$userOld) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if (!$userNew) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$this->repository->mergeUser($userOld, $userNew);
return $this->eadResponse([ "success" => 1 ]);
}
/**
* @Route(
* path = "/admin/user/register",
* name = "userRegister",
* methods = {"POST"},
* )
*/
public function registerUser(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "create");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$name = $this->requestUtil->getField('name');
$email = $this->requestUtil->getField('email');
$password = $this->requestUtil->getField('password');
$userProfileId = $this->requestUtil->getField('userProfile');
$groupId = $this->requestUtil->getField('group');
$totalEmail = $this->repository->countByEmail($email);
if(!empty($totalEmail)){
return $this->eadResponse([ 'email' ], ErrorEnum::FIELD_EMPTY);
}
$allowPermissions = [
UserEnum::STUDENT
];
$permission = $this->userPermissionUtil->getPermission("user", "permissions");
if($this->userPermissionUtil->isHigh($permission)){
$maxAllowID = $this->user->getUserProfile()->getId();
$allowPermissions[] = $maxAllowID;
if($maxAllowID == UserEnum::ADMIN){
$allowPermissions[] = UserEnum::TUTOR;
$allowPermissions[] = UserEnum::TEACHER;
$allowPermissions[] = $userProfileId;
}else if($maxAllowID == UserEnum::TEACHER){
$allowPermissions[] = UserEnum::TUTOR;
}
}
if(!in_array($userProfileId, $allowPermissions)){
$userProfileId = null;
}
$dataUser = [
"name" => $name,
"email" => $email,
"password" => $password,
"userProfileId" => $userProfileId,
"groupId" => $groupId,
"notify" => UserEnum::YES,
"invited" => UserEnum::YES
];
$userDTO = new UserDTO($dataUser);
$data = $this->repository->newUser($userDTO);
if($data->errors){
return $this->eadResponse($data->errors, ErrorEnum::FIELD_EMPTY);
}
$user = $data->user;
$data = $user->toReturn();
$this->userLogService->logInsert("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/edit/info/cart/{id}",
* name = "userEditInfoCart",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function editUserInfoCart(Request $request) {
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($userId != $this->user->getId()){
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$this->requestUtil->setRequest($request)->setData();
if($this->requestUtil->issetField('document')){
$user->setDocument($this->requestUtil->getField('document'));
}
if($this->requestUtil->issetField('country')){
$countryId = $this->requestUtil->getField('country');
$country = $this->em->getRepository(Country::class)->findOneBy([
"id" => $countryId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($country){
$user->setCountry($country);
}else{
$user->setCountry(null);
}
}
if($this->requestUtil->issetField('state')){
$stateId = $this->requestUtil->getField('state');
$state = $this->em->getRepository(State::class)->findOneBy([
"id" => $stateId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($state){
$user->setState($state);
}else{
$user->setState(null);
}
}
if($this->requestUtil->issetField('city')){
$cityId = $this->requestUtil->getField('city');
$city = $this->em->getRepository(City::class)->findOneBy([
"id" => $cityId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($city){
$user->setCity($city);
}else{
$user->setCity(null);
}
}
if($this->requestUtil->issetField('zipCode')){
$user->setZipCode($this->requestUtil->getField('zipCode'));
}
if($this->requestUtil->issetField('address')){
$user->setAddress($this->requestUtil->getField('address'));
}
if($this->requestUtil->issetField('addressNumber')){
$user->setAddressNumber($this->requestUtil->getField('addressNumber'));
}
if($this->requestUtil->issetField('addressComplement')){
$user->setAddressComplement($this->requestUtil->getField('addressComplement'));
}
if($this->requestUtil->issetField('addressNeighborhood')){
$user->setAddressNeighborhood(
$this->requestUtil->getField('addressNeighborhood')
);
}
$errors = $this->validateEntity($user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$userCheckoutInfoRepository = $this->em->getRepository(UserCheckoutInfo::class);
$userCheckoutInfoId = null;
$userCheckoutInfo = $userCheckoutInfoRepository->findOneBy([
"user" => $user->getId(),
"default" => UserCheckoutInfoEnum::YES,
"deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED
]);
$new = false;
if(!$userCheckoutInfo){
$new = true;
$userCheckoutInfo = new UserCheckoutInfo();
}
$userCheckoutInfo->setName($user->getName());
$userCheckoutInfo->setEmail($user->getEmail());
$userCheckoutInfo->setDocument($user->getDocument());
$userCheckoutInfo->setPhone($user->getPhone());
$userCheckoutInfo->setZipCode($user->getZipCode());
$userCheckoutInfo->setAddress($user->getAddress());
$userCheckoutInfo->setAddressNumber($user->getAddressNumber());
$userCheckoutInfo->setAddressComplement($user->getAddressComplement());
$userCheckoutInfo->setAddressNeighborhood($user->getAddressNeighborhood());
$userCheckoutInfo->setCity($user->getCity());
$userCheckoutInfo->setState($user->getState());
$userCheckoutInfo->setCountry($user->getCountry());
$userCheckoutInfo->setUser($user);
$userCheckoutInfo->setReceiveEmail(UserCheckoutInfoEnum::NO);
if($new){
$userCheckoutInfo->setDefault(UserCheckoutInfoEnum::YES);
$this->em->persist($userCheckoutInfo);
}
$userCheckoutInfoId = $userCheckoutInfo->getId();
$this->em->flush();
$this->repository->sendWebhook($user);
$crmService = $this->generalService->getService('CRM\\CrmService');
$crmService->savePerson($user);
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
$data['userCheckoutInfoId'] = $userCheckoutInfoId;
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/edit/info/profile",
* name = "userEditInfoProfile",
* methods = {"POST"}
* )
*/
public function userEditInfoProfile(Request $request) {
if (!$this->user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$this->requestUtil->setRequest($request)->setData();
$files = $this->requestUtil->getFile('files');
$filePhoto = $this->requestUtil->getFile('photo');
$fileCover = $this->requestUtil->getFile('cover');
$emailService = $this->generalService->getService('EmailService');
$emailService->setToEmail($this->user->getEmail());
$emailService->setToName($this->user->getName());
if($filePhoto){
if($filePhoto->getSize() > UserEnum::MAX_SIZE_USER_IMAGES){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
}
}
if($fileCover){
if($fileCover->getSize() > UserEnum::MAX_SIZE_USER_IMAGES){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
}
}
if($this->requestUtil->issetField('name')){
$this->user->setName($this->requestUtil->getField('name'));
}
if($this->requestUtil->issetField('username')){
if($this->user->getUsername() != $this->requestUtil->getField('username'))
{
$totalUsername = $this->repository->countByUsername($this->requestUtil->getField('username'));
if(!empty($totalUsername))
{
return $this->eadResponse([ 'username' ], ErrorEnum::FIELD_EMPTY);
}
$this->user->setUsername($this->requestUtil->getField('username'));
}
}
$password = "******";
if($this->requestUtil->issetField('password')){
$newPassword = $this->requestUtil->getField('password');
if(
$this->user->getPassword() != $newPassword &&
$this->user->getPassword() != md5($newPassword)
){
$oldPassword = $this->user->getPassword();
$this->user->setPassword($newPassword);
$oldPass = $this->user->getOldPassword(true);
if(in_array($this->user->getPassword(), $oldPass)){
return $this->eadResponse([
"message" => "Nova senha precisa ser diferente da anterior!"
], ErrorEnum::ACTION_INVALID);
}
if($oldPassword != UserEnum::PASSWORD_RESET){
$this->user->setOldPassword($oldPassword);
}
if(
$emailService->checkUserToSend($this->user, false) ||
$this->configuration->get("allow_send_email_user") == UserEnum::YES
){
$subText = $this->configuration->getLanguage(
'change_password.subject', 'email'
);
$subject = "{$subText} - {$this->client->getBrand()}";
$emailService->setSubject($subject);
$emailService->setData([
"userName" => $this->user->getName(),
"btnLink" => 'https://'.$this->client->getDomainPrimary()."/profile",
]);
$emailService->setTemplateBody("change_password");
$emailService->send();
}
}
}
if($this->requestUtil->issetField('document')){
$this->user->setDocument($this->requestUtil->getField('document'));
}
if($this->requestUtil->issetField('phone')){
$this->user->setPhone($this->requestUtil->getField('phone'));
}
if($this->requestUtil->issetField('birthDate')){
$birthDate = $this->requestUtil->getField('birthDate');
$birthDate = implode('-', array_reverse(explode('/', $birthDate)));
$this->user->setBirthDate($birthDate);
}
if($this->configuration->checkModuleIsAbleOnPlan('customFormModule')){
if($this->requestUtil->issetField('customField')){
$this->user->setCustomField($this->requestUtil->getField('customField'));
}
}
if($this->requestUtil->issetField('occupation')){
$this->user->setOccupation($this->requestUtil->getField('occupation'));
}
if($this->requestUtil->issetField('biography')){
$this->user->setBiography($this->requestUtil->getField('biography'));
}
if($this->requestUtil->issetField('resume')){
$this->user->setResume($this->requestUtil->getField('resume'));
}
if($this->requestUtil->issetField('custom')){
$this->user->setCustom($this->requestUtil->getField('custom'));
}
if($this->requestUtil->issetField('notes')){
$this->user->setNotes($this->requestUtil->getField('notes'));
}
if($this->requestUtil->issetField('country')){
$countryId = $this->requestUtil->getField('country');
$country = $this->em->getRepository(Country::class)->findOneBy([
"id" => $countryId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($country){
$this->user->setCountry($country);
}else{
$this->user->setCountry(null);
}
}
if($this->requestUtil->issetField('state')){
$stateId = $this->requestUtil->getField('state');
$state = $this->em->getRepository(State::class)->findOneBy([
"id" => $stateId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($state){
$this->user->setState($state);
}else{
$this->user->setState(null);
}
}
if($this->requestUtil->issetField('city')){
$cityId = $this->requestUtil->getField('city');
$city = $this->em->getRepository(City::class)->findOneBy([
"id" => $cityId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($city){
$this->user->setCity($city);
}else{
$this->user->setCity(null);
}
}
if($this->requestUtil->issetField('zipCode')){
$this->user->setZipCode($this->requestUtil->getField('zipCode'));
}
if($this->requestUtil->issetField('address')){
$this->user->setAddress($this->requestUtil->getField('address'));
}
if($this->requestUtil->issetField('addressNumber')){
$this->user->setAddressNumber($this->requestUtil->getField('addressNumber'));
}
if($this->requestUtil->issetField('addressComplement')){
$this->user->setAddressComplement(
$this->requestUtil->getField('addressComplement')
);
}
if($this->requestUtil->issetField('addressNeighborhood')){
$this->user->setAddressNeighborhood(
$this->requestUtil->getField('addressNeighborhood')
);
}
if($this->requestUtil->issetField('website')){
$this->user->setWebsite($this->requestUtil->getField('website'));
}
if($this->requestUtil->issetField('twitter')){
$this->user->setTwitter($this->requestUtil->getField('twitter'));
}
if($this->requestUtil->issetField('facebook')){
$this->user->setFacebook($this->requestUtil->getField('facebook'));
}
if($this->requestUtil->issetField('linkedin')){
$this->user->setLinkedin($this->requestUtil->getField('linkedin'));
}
if($this->requestUtil->issetField('youtube')){
$this->user->setYoutube($this->requestUtil->getField('youtube'));
}
if($this->requestUtil->issetField('instagram')){
$this->user->setInstagram($this->requestUtil->getField('instagram'));
}
if($this->requestUtil->issetField('tiktok')){
$this->user->setTiktok($this->requestUtil->getField('tiktok'));
}
$errors = $this->validateEntity($this->user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
if($filePhoto){
$filePhoto = $this->fileService->setFile($filePhoto);
if($filePhoto){
$this->fileService->moveFile(UserEnum::PATH_PROFILES);
$this->user->setPhoto($this->fileService->getFileName());
}
}
if($fileCover){
$fileCover = $this->fileService->setFile($fileCover);
if($fileCover){
$this->fileService->moveFile(UserEnum::PATH_COVERS_PROFILE);
$this->user->setCover($this->fileService->getFileName());
}
}
if($this->configuration->checkModuleIsAbleOnPlan('customFormModule')){
$customField = json_decode($this->user->getCustomField());
if($customField){
foreach ($customField as $key => $field) {
if(!empty($field->fieldId)){
if(isset($files[$field->fieldId])){
$file = $files[$field->fieldId];
if(
$field->type == UserCustomFieldEnum::FIELD_FILE
&& $file->getError() == UserCustomFieldEnum::NO
){
$file = $this->fileService->setFile($file);
if($file){
$this->fileService->moveFile(
UserEnum::PATH_OTHERS_PROFILES
);
$filePath = $this->fileService->getFileName();
$extension = $this->fileService->getFileExtension();
if(!empty($field->value)){
$field->value->path = $filePath;
$field->value->extension = $extension;
}
$customField->{$key} = $field;
}
}
}
}
}
$customField = json_encode($customField);
$this->user->setCustomField($customField);
}
}
$this->em->flush();
$this->repository->sendWebhook($this->user);
$crmService = $this->generalService->getService('CRM\\CrmService');
$crmService->savePerson($this->user);
$data = $this->user->toReturn();
$this->userLogService->logUpdate("user", $this->user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/edit/lesson/config",
* name = "userEditLessonConfig",
* methods = {"PUT"}
* )
*/
public function userEditLessonConfig(Request $request) {
if (!$this->user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$this->requestUtil->setRequest($request)->setData();
if($this->requestUtil->issetField('screenSize')){
$this->user->setScreenSize((int)$this->requestUtil->getField('screenSize'));
}
if($this->requestUtil->issetField('autoplay')){
$this->user->setAutoplay((int)$this->requestUtil->getField('autoplay'));
}
$errors = $this->validateEntity($this->user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
$data = $this->user->toReturn();
$this->userLogService->logUpdate("user", $this->user->getId(), $data);
return $this->eadResponse([ "success" => 1 ]);
}
/**
* @Route(
* path = "/admin/user/edit/payment/config",
* name = "userEditPaymentConfig",
* methods = {"PUT"}
* )
*/
public function userEditPaymentConfig(Request $request) {
if (!$this->user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$this->requestUtil->setRequest($request)->setData();
if($this->requestUtil->issetField('allowOneClickBuy')){
$this->user->setAllowOneClickBuy(
(int)$this->requestUtil->getField('allowOneClickBuy')
);
}
$errors = $this->validateEntity($this->user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
$data = $this->user->toReturn();
$this->userLogService->logUpdate("user", $this->user->getId(), $data);
return $this->eadResponse([ "success" => 1 ]);
}
/**
* @Route(
* path = "/admin/user/edit/info/{id}",
* name = "userEditInfo",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function editUserInfo(Request $request) {
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($userId != $this->user->getId()){
$permission = $this->userPermissionUtil->getPermission("user", "edit");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
}
$emailService = $this->generalService->getService('EmailService');
$emailService->setToEmail($user->getEmail());
$emailService->setToName($user->getName());
$this->requestUtil->setRequest($request)->setData();
$userProfileSave = $user->getUserProfile();
$userOldStatus = $user->getOldStatus();
$userTypeSave = $user->getType();
$userAllowReceiverSave = $user->getAllowReceiver();
if($this->requestUtil->issetField('email'))
{
if($user->getEmail() != $this->requestUtil->getField('email'))
{
$totalEmail = $this->repository->countByEmail($this->requestUtil->getField('email'));
if(!empty($totalEmail))
{
return $this->eadResponse([ 'email' ], ErrorEnum::FIELD_EMPTY);
}
$user->setEmail(strtolower($this->requestUtil->getField('email')));
$user->setValidEmail(UserEnum::UNKNOWN);
}
}
if($this->requestUtil->issetField('name')){
$user->setName($this->requestUtil->getField('name'));
}
if($this->requestUtil->issetField('username')){
if($user->getUsername() != $this->requestUtil->getField('username'))
{
$totalUsername = $this->repository->countByUsername(
$this->requestUtil->getField('username')
);
if(!empty($totalUsername))
{
return $this->eadResponse([ 'username' ], ErrorEnum::FIELD_EMPTY);
}
$user->setUsername($this->requestUtil->getField('username'));
}
}
if($this->requestUtil->issetField('teacherSpotlight')){
$user->setTeacherSpotlight((int)$this->requestUtil->getField('teacherSpotlight'));
$this->em->getRepository(Configuration::class)->resetTeacherNumber();
}
$updatePassword = false;
$password = "******";
if($this->requestUtil->issetField('password')){
$newPassword = $this->requestUtil->getField('password');
if(
$user->getPassword() != $newPassword &&
$user->getPassword() != md5($newPassword)
){
$oldPassword = $user->getPassword();
$user->setPassword($newPassword);
$oldPass = $user->getOldPassword(true);
if(in_array($user->getPassword(), $oldPass)){
return $this->eadResponse([
"message" => "Nova senha precisa ser diferente da anterior!"
], ErrorEnum::ACTION_INVALID);
}
$updatePassword = true;
if($oldPassword != UserEnum::PASSWORD_RESET){
$user->setOldPassword($oldPassword);
}
}
}
if($this->requestUtil->issetField('allowReceiver')){
$user->setAllowReceiver((int)$this->requestUtil->getField('allowReceiver'));
}
if($this->requestUtil->issetField('document')){
$user->setDocument($this->requestUtil->getField('document'));
}
if($this->requestUtil->issetField('birthDate')){
$user->setBirthDate($this->requestUtil->getField('birthDate'));
}
if($this->requestUtil->issetField('phone')){
$user->setPhone($this->requestUtil->getField('phone'));
}
if($this->requestUtil->issetField('userProfile')){
$userProfileId = $this->requestUtil->getField('userProfile');
$maxAllowID = $this->user->getUserProfile()->getId();
$saveAllowID = $user->getUserProfile()->getId();
$allowEditPermissions = (
$maxAllowID == $saveAllowID ||
$maxAllowID == UserEnum::ADMIN ||
$saveAllowID == UserEnum::STUDENT
);
if($saveAllowID == UserEnum::ADMIN){
$allowEditPermissions = ($maxAllowID == $saveAllowID);
}else if($saveAllowID == UserEnum::TEACHER){
$allowEditPermissions = ($maxAllowID == UserEnum::ADMIN);
}else if($saveAllowID == UserEnum::TUTOR){
$allowEditPermissions = (
$maxAllowID == UserEnum::TEACHER || $maxAllowID == UserEnum::ADMIN
);
}
if($userProfileSave->getId() != $userProfileId && $allowEditPermissions){
$allowPermissions = [
$maxAllowID,
UserEnum::STUDENT
];
if($maxAllowID == UserEnum::ADMIN){
$allowPermissions[] = UserEnum::TUTOR;
$allowPermissions[] = UserEnum::TEACHER;
$allowPermissions[] = $userProfileId;
}else if($maxAllowID == UserEnum::TEACHER){
$allowPermissions[] = UserEnum::TUTOR;
}
$permission = $this->userPermissionUtil->getPermission("user", "permissions");
if(!$this->userPermissionUtil->isHigh($permission)){
if(!in_array($userProfileId, $allowPermissions)){
$userProfileId = null;
}
}
$userProfile = $this->em->getRepository(UserProfile::class)->findOneBy([
"id" => $userProfileId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
$user->setUserProfile($userProfile);
if($userProfile){
$user->setPermission($userProfile->getPermission());
}
}
}
if($this->requestUtil->issetField('type')){
$type = (int)$this->requestUtil->getField('type');
$user->setType($type);
if($userTypeSave == UserEnum::ADMIN && $userTypeSave != $type){
$adminsNumber = $this->repository->countAdmins();
if($adminsNumber > 1){
$user->setType(null);
}
}
}
if($this->requestUtil->issetField('status')){
$user->setStatus((int)$this->requestUtil->getField('status'));
if($user->getStatus() == UserEnum::ACTIVE){
$user->setConfirmationDate(date('Y-m-d H:i:s'));
}
}
if($this->requestUtil->issetField('validEmail')){
$user->setValidEmail($this->requestUtil->getField('validEmail'));
}
if($user->getUserProfile()){
$types = [
UserEnum::STUDENT,
UserEnum::TUTOR,
UserEnum::TEACHER,
UserEnum::ADMIN,
];
if(in_array($user->getUserProfile()->getId(), $types)){
$user->setType($user->getUserProfile()->getId());
}
}
$errors = $this->validateEntity($user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
if($updatePassword){
$this->em->getRepository(Session::class)->resetSessions($user);
if(
$emailService->checkUserToSend($user, false) ||
$this->configuration->get("allow_send_email_user") == UserEnum::YES
){
$subText = $this->configuration->getLanguage(
'change_password.subject', 'email'
);
$subject = "{$subText} - {$this->client->getBrand()}";
$emailService->setSubject($subject);
$emailService->setData([
"userName" => $user->getName(),
"btnLink" => "https://{$this->client->getDomainPrimary()}/profile",
]);
$emailService->setTemplateBody("change_password");
$emailService->send();
}
}
$this->em->flush();
$crmService = $this->generalService->getService('CRM\\CrmService');
$crmService->savePerson($user);
$marketingService = $this->generalService->getService(
'Marketing\\MarketingService'
);
if($user->getStatus() == UserEnum::ACTIVE && $userOldStatus == UserEnum::WAITING){
$marketingService->setTag(TagsMarketingEnum::TAG_CONFIRM_REGISTER);
$marketingService->setUser($user);
$marketingService->send();
}
$userPermission = json_decode($user->getPermission());
if($userAllowReceiverSave != $user->getAllowReceiver()){
if($user->getAllowReceiver() == UserEnum::NO){
if(!isset($userPermission->financial)){
$userPermission->financial = (object)[];
}
if(!isset($userPermission->financial->receiver)){
$userPermission->financial->receiver = (object)[];
}
if(empty($userPermission->financial->receiver->see)){
$userPermission->financial->receiver->see = UserPermissionEnum::LOW;
}
if(empty($userPermission->financial->receiver->enable)){
$userPermission->financial->receiver->enable = UserPermissionEnum::LOW;
}
if(empty($userPermission->financial->receiver->disable)){
$userPermission->financial->receiver->disable = UserPermissionEnum::LOW;
}
if(empty($userPermission->financial->receiver->create)){
$userPermission->financial->receiver->create = UserPermissionEnum::LOW;
}
if(empty($userPermission->financial->receiver->edit)){
$userPermission->financial->receiver->edit = UserPermissionEnum::LOW;
}
if(empty($userPermission->financial->receiver->delete)){
$userPermission->financial->receiver->delete = UserPermissionEnum::LOW;
}
$receiver = $this->em->getRepository(Receiver::class)->findOneBy([
"user" => $user,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($receiver){
$receiver->setStatus(ReceiverEnum::INACTIVE);
}
}else if($user->getAllowReceiver() == UserEnum::YES){
if(!isset($userPermission->financial)){
$userPermission->financial = (object)[];
}
if(!isset($userPermission->financial->receiver)){
$userPermission->financial->receiver = (object)[];
}
if(empty($userPermission->financial->receiver->see)){
$userPermission->financial->receiver->see = UserPermissionEnum::MIDDLE;
}
if(empty($userPermission->financial->receiver->enable)){
$userPermission->financial->receiver->enable = UserPermissionEnum::MIDDLE;
}
if(empty($userPermission->financial->receiver->disable)){
$userPermission->financial->receiver->disable = UserPermissionEnum::MIDDLE;
}
if(empty($userPermission->financial->receiver->create)){
$userPermission->financial->receiver->create = UserPermissionEnum::MIDDLE;
}
if(empty($userPermission->financial->receiver->edit)){
$userPermission->financial->receiver->edit = UserPermissionEnum::MIDDLE;
}
if(empty($userPermission->financial->receiver->delete)){
$userPermission->financial->receiver->delete = UserPermissionEnum::MIDDLE;
}
if($emailService->checkUserToSend($user)){
//send email allow receiver
$subText = $this->configuration->getLanguage(
'receiver_register.subject', 'email'
);
$subject = "{$subText} - {$this->client->getBrand()}";
$emailService->setSubject($subject);
$emailService->setData([
"userName" => $user->getName(),
"btnLink" => "https://{$this->client->getDomainPrimary()}/profile#table-receivers",
]);
$emailService->setTemplateBody("receiver_register");
$emailService->send();
}
}
}
$user->setPermission(json_encode($userPermission));
$this->em->flush();
$this->repository->sendWebhook($user);
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/enable/authentication",
* name = "userEnableAuthentication",
* methods = {"PUT"}
* )
*/
public function userEnableAuthentication(Request $request) {
if (!$this->user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$this->requestUtil->setRequest($request)->setData();
$secretKey = $this->requestUtil->getField('secretKey');
$code = $this->requestUtil->getField('code');
$googleAuthenticatorService = $this->generalService->getService('GoogleAuthenticatorService');
$googleAuthenticatorService->setSecretKey($secretKey);
$googleAuthenticatorService->setCode($code);
$checkCode = $googleAuthenticatorService->checkCodeAuthentication();
$this->user->setAuthenticationSecretKey($secretKey);
$this->user->setAuthenticationAllow(UserEnum::YES);
$errors = $this->validateEntity($this->user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
if(!$checkCode){
return $this->eadResponse($errors, ErrorEnum::AUTH_INVALID);
}
$this->em->flush();
$data = $this->user->toReturn();
$this->userLogService->logUpdate("user", $this->user->getId(), $data);
return $this->eadResponse([ "success" => 1 ]);
}
/**
* @Route(
* path = "/admin/user/disable/authentication",
* name = "userDisableAuthentication",
* methods = {"PUT"}
* )
*/
public function userDisableAuthentication(Request $request) {
if (!$this->user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$this->requestUtil->setRequest($request)->setData();
$this->user->setAuthenticationSecretKey(null);
$this->user->setAuthenticationCodeEmail(null);
$this->user->setAuthenticationEmailDateExpire(null);
$this->user->setAuthenticationAllow(UserEnum::NO);
$errors = $this->validateEntity($this->user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
$data = $this->user->toReturn();
$this->userLogService->logUpdate("user", $this->user->getId(), $data);
return $this->eadResponse([ "success" => 1 ]);
}
/**
* @Route(
* path = "/admin/user/edit/permission/{id}",
* name = "userEditPermission",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function editUserPermission(Request $request) {
if(!$this->configuration->checkModuleIsAbleOnPlan('permissionFunction')){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
}
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$permission = $this->userPermissionUtil->getPermission("user", "permissions");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
if($this->requestUtil->issetField('permission')){
$user->setPermission($this->requestUtil->getField('permission'));
}
$errors = $this->validateEntity($user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/edit/custom/{id}",
* name = "userEditCustom",
* methods = {"POST"},
* requirements = { "id" = "\d+" }
* )
*/
public function editUserCustomFields(Request $request) {
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($userId != $this->user->getId()){
$permission = $this->userPermissionUtil->getPermission("user", "edit");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
}
$this->requestUtil->setRequest($request)->setData();
$files = $this->requestUtil->getFile('file');
if($this->configuration->checkModuleIsAbleOnPlan('customFormModule')){
if($this->requestUtil->issetField('customField')){
$user->setCustomField($this->requestUtil->getField('customField'));
}
}
if($this->requestUtil->issetField('occupation')){
$user->setOccupation($this->requestUtil->getField('occupation'));
}
if($this->requestUtil->issetField('biography')){
$user->setBiography($this->requestUtil->getField('biography'));
}
if($this->requestUtil->issetField('resume')){
$user->setResume($this->requestUtil->getField('resume'));
}
if($this->requestUtil->issetField('custom')){
$user->setCustom($this->requestUtil->getField('custom'));
}
if($this->requestUtil->issetField('notes')){
$user->setNotes($this->requestUtil->getField('notes'));
}
$errors = $this->validateEntity($user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
if($this->configuration->checkModuleIsAbleOnPlan('customFormModule')){
$customField = json_decode($user->getCustomField());
if($customField){
foreach ($customField as $key => $field) {
if(!empty($field->fieldId)){
if(isset($files[$field->fieldId])){
$file = $files[$field->fieldId];
if(
$field->type == UserCustomFieldEnum::FIELD_FILE &&
$file->getError() == UserCustomFieldEnum::NO
){
$file = $this->fileService->setFile($file);
if($file){
$this->fileService->moveFile(
UserEnum::PATH_OTHERS_PROFILES
);
$filePath = $this->fileService->getFileName();
$extension = $this->fileService->getFileExtension();
if(!empty($field->value)){
$field->value->path = $filePath;
$field->value->extension = $extension;
}
$customField->{$key} = $field;
}
}
}
}
}
$customField = json_encode($customField);
$user->setCustomField($customField);
}
}
$this->em->flush();
$this->repository->sendWebhook($user);
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/edit/address/{id}",
* name = "userEditAddress",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function editUserAddress(Request $request) {
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($userId != $this->user->getId()){
$permission = $this->userPermissionUtil->getPermission("user", "edit");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
}
$this->requestUtil->setRequest($request)->setData();
if($this->requestUtil->issetField('country')){
$countryId = $this->requestUtil->getField('country');
$country = $this->em->getRepository(Country::class)->findOneBy([
"id" => $countryId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
$user->setCountry($country);
}
if($this->requestUtil->issetField('state')){
$stateId = $this->requestUtil->getField('state');
$state = $this->em->getRepository(State::class)->findOneBy([
"id" => $stateId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
$user->setState($state);
}
if($this->requestUtil->issetField('city')){
$cityId = $this->requestUtil->getField('city');
$city = $this->em->getRepository(City::class)->findOneBy([
"id" => $cityId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
$user->setCity($city);
}
if($this->requestUtil->issetField('zipCode')){
$user->setZipCode($this->requestUtil->getField('zipCode'));
}
if($this->requestUtil->issetField('address')){
$user->setAddress($this->requestUtil->getField('address'));
}
if($this->requestUtil->issetField('addressNumber')){
$user->setAddressNumber($this->requestUtil->getField('addressNumber'));
}
if($this->requestUtil->issetField('addressComplement')){
$user->setAddressComplement($this->requestUtil->getField('addressComplement'));
}
if($this->requestUtil->issetField('addressNeighborhood')){
$user->setAddressNeighborhood(
$this->requestUtil->getField('addressNeighborhood')
);
}
$errors = $this->validateEntity($user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
$this->repository->sendWebhook($user);
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/edit/social/{id}",
* name = "userEditSocial",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function editUserSocial(Request $request) {
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($userId != $this->user->getId()){
$permission = $this->userPermissionUtil->getPermission("user", "edit");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
}
$this->requestUtil->setRequest($request)->setData();
if($this->requestUtil->issetField('website')){
$user->setWebsite($this->requestUtil->getField('website'));
}
if($this->requestUtil->issetField('twitter')){
$user->setTwitter($this->requestUtil->getField('twitter'));
}
if($this->requestUtil->issetField('facebook')){
$user->setFacebook($this->requestUtil->getField('facebook'));
}
if($this->requestUtil->issetField('linkedin')){
$user->setLinkedin($this->requestUtil->getField('linkedin'));
}
if($this->requestUtil->issetField('youtube')){
$user->setYoutube($this->requestUtil->getField('youtube'));
}
if($this->requestUtil->issetField('instagram')){
$user->setInstagram($this->requestUtil->getField('instagram'));
}
if($this->requestUtil->issetField('tiktok')){
$this->user->setTiktok($this->requestUtil->getField('tiktok'));
}
$errors = $this->validateEntity($user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
$this->repository->sendWebhook($user);
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/edit/theme/{id}",
* name = "userEditTheme",
* methods = {"POST"},
* requirements = { "id" = "\d+" }
* )
*/
public function editUserTheme(Request $request) {
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($userId != $this->user->getId()){
$permission = $this->userPermissionUtil->getPermission("user", "edit");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
}
$this->requestUtil->setRequest($request)->setData();
$filePhoto = $this->requestUtil->getFile('photo');
$fileCover = $this->requestUtil->getFile('cover');
if($filePhoto){
if($filePhoto->getSize() > UserEnum::MAX_SIZE_USER_IMAGES){
return $this->eadResponse([
"message" => "Photo Size"
], ErrorEnum::ACTION_INVALID);
}
$filePhoto = $this->fileService->setFile($filePhoto);
if($filePhoto){
$this->fileService->moveFile(UserEnum::PATH_PROFILES);
$user->setPhoto($this->fileService->getFileName());
}
}
if($fileCover){
if($fileCover->getSize() > UserEnum::MAX_SIZE_USER_IMAGES){
return $this->eadResponse([
"message" => "Cover Size"
], ErrorEnum::ACTION_INVALID);
}
$fileCover = $this->fileService->setFile($fileCover);
if($fileCover){
$this->fileService->moveFile(UserEnum::PATH_COVERS_PROFILE);
$user->setCover($this->fileService->getFileName());
}
}
$errors = $this->validateEntity($user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/edit/notification/{id}",
* name = "userEditNotification",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function editUserNotification(Request $request) {
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($userId != $this->user->getId()){
$permission = $this->userPermissionUtil->getPermission("user", "edit");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
}
$this->requestUtil->setRequest($request)->setData();
if($this->requestUtil->issetField('allowNotifyNewLesson')){
$user->setAllowNotifyNewLesson($this->requestUtil->getField('allowNotifyNewLesson'));
}
if($this->requestUtil->issetField('allowNotifyNewExam')){
$user->setAllowNotifyNewExam(
$this->requestUtil->getField('allowNotifyNewExam')
);
}
if($this->requestUtil->issetField('allowNotifyNewSupportMessage')){
$user->setAllowNotifyNewSupportMessage(
$this->requestUtil->getField('allowNotifyNewSupportMessage')
);
}
if($this->requestUtil->issetField('allowNotifyNewSupportAnswer')){
$user->setAllowNotifyNewSupportAnswer(
$this->requestUtil->getField('allowNotifyNewSupportAnswer')
);
}
if($this->requestUtil->issetField('allowNotifyNewMessage')){
$user->setAllowNotifyNewMessage(
$this->requestUtil->getField('allowNotifyNewMessage')
);
}
if($this->requestUtil->issetField('allowNotifyNewGroupMessage')){
$user->setAllowNotifyNewGroupMessage(
$this->requestUtil->getField('allowNotifyNewGroupMessage')
);
}
if($this->requestUtil->issetField('allowNotifyCart')){
$user->setAllowNotifyCart($this->requestUtil->getField('allowNotifyCart'));
}
$this->em->flush();
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/edit/status/email/{id}",
* name = "userEditStatusEmail",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function editUserStatusEmail(Request $request) {
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($userId != $this->user->getId()){
$permission = $this->userPermissionUtil->getPermission("user", "edit");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
}
$this->requestUtil->setRequest($request)->setData();
if($this->requestUtil->issetField('validEmail')){
$user->setValidEmail($this->requestUtil->getField('validEmail'));
}
$this->em->flush();
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/edit/many/status/email",
* name = "userEditManyStatusEmail",
* methods = {"PUT"}
* )
*/
public function editManyUserStatusEmail(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "edit");
$this->requestUtil->setRequest($request)->setData();
$userId = $this->requestUtil->getField('users');
if(empty($userId)){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
};
$userId = json_decode($userId);
foreach ($userId as $key => $id) {
$user = $this->repository->findOneBy([
"id" => $id,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($user) {
if($this->userPermissionUtil->isHigh($permission)){
if($this->requestUtil->issetField('validEmail')){
$user->setValidEmail((int)$this->requestUtil->getField('validEmail'));
}
$errors = $this->validateEntity($user);
if(!$errors){
$this->em->flush();
$data = $user->toReturn();
$this->userLogService->logUpdate(
"user",
$user->getId(),
$data
);
}
}
}
}
return $this->eadResponse([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/user/delete/custom/file/{id}/{fieldId}",
* name = "userDeleteCustomFile",
* methods = {"DELETE"},
* requirements = { "id" = "\d+", "fieldId" = "\d+" }
* )
*/
public function deleteCustomFieldFile(Request $request) {
if(!$this->configuration->checkModuleIsAbleOnPlan('customFormModule')){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
}
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($userId != $this->user->getId()){
$permission = $this->userPermissionUtil->getPermission("user", "edit");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
}
$fieldId = $request->get('fieldId');
$fields = json_decode($user->getCustomField());
if(!empty($fields->{$fieldId})){
if(!empty($fields->{$fieldId}->value)){
$file = $fields->{$fieldId}->value->path;
$pathComplete = $this->fileService->getFilePathComplete(
$file,
UserEnum::PATH_OTHERS_PROFILES
);
$this->fileService->setFile($pathComplete);
$this->fileService->deleteFile();
$fields->{$fieldId}->value = null;
}
//unset($fields->{$fieldId});
$user->setCustomField(json_encode($fields));
}
$this->em->flush();
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse([ "success" => 1 ]);
}
/**
* @Route(
* path = "/admin/user/delete/cover/{id}",
* name = "userDeleteCover",
* methods = {"DELETE"},
* requirements = { "id" = "\d+" }
* )
*/
public function deleteUserCover(Request $request) {
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($userId != $this->user->getId()){
$permission = $this->userPermissionUtil->getPermission("user", "edit");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
}
if($user->getCover()){
$pathComplete = $this->fileService->getFilePathComplete(
$user->getCover(),
UserEnum::PATH_COVERS_PROFILE
);
$this->fileService->setFile($pathComplete);
$this->fileService->deleteFile();
$user->setCover(null);
}
$this->em->flush();
$return = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $return);
return $this->eadResponse($return);
}
/**
* @Route(
* path = "/admin/user/delete/photo/{id}",
* name = "userDeletePhoto",
* methods = {"DELETE"},
* requirements = { "id" = "\d+" }
* )
*/
public function deleteUserPhoto(Request $request) {
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($userId != $this->user->getId()){
$permission = $this->userPermissionUtil->getPermission("user", "edit");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
}
if($user->getPhoto()){
$pathComplete = $this->fileService->getFilePathComplete(
$user->getPhoto(),
UserEnum::PATH_PROFILES
);
$this->fileService->setFile($pathComplete);
$this->fileService->deleteFile();
$user->setPhoto(null);
}
$this->em->flush();
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/recover/data",
* name = "getDataToRecover",
* methods = {"GET"}
* )
*/
public function getDataToRecover(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "recover");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$data = [
"notConfirmNumber" => $this->repository->countNotConfirmed(),
"confirmationSendNumber" => $this->repository->countRecoverSendToday(),
"recoverNumber" => $this->repository->countRecoverToday(),
];
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/recover",
* name = "userRecover",
* methods = {"GET"}
* )
*/
public function recoverUser(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "recover");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$user = $this->repository->getUserToRecover();
if(!$user){
return $this->eadResponse([ "continue" => false ], ErrorEnum::NOT_FOUND);
}
if($user->getRecoverAttempt() >= 4){
return $this->eadResponse([ "continue" => false ], ErrorEnum::ACTION_INVALID);
}
$emailService = $this->generalService->getService('EmailService');
$password = "******";
if($user->getInvited() == UserEnum::YES){
$password = $this->stringUtil->randomText(6);
$user->setPassword($password);
$user->setOldPassword($user->getPassword());
}
$user->setRecoverDate(date('Y-m-d'));
$send = false;
if(
$emailService->checkUserToSend($user, false) ||
$this->configuration->get("allow_send_email_user") == UserEnum::YES
){
$emailService->setToEmail($user->getEmail());
$emailService->setToName($user->getName());
$subText = $this->configuration->getLanguage('recover_user.subject', 'email');
$subject = "{$subText} - {$this->client->getBrand()}";
$emailService->setSubject($subject);
$domain = $this->client->getDomainPrimary();
$emailService->setData([
"userName" => $user->getName(),
"invited" => $user->getInvited(),
"userDateRegister" => $user->getDateRegister(),
"userEmail" => $user->getEmail(),
"userPassword" => $password,
"btnConfirm" => "https://{$domain}/confirm/{$user->getHashIdentify()}",
"btnDelete" => "https://{$domain}/stopEmail/{$user->getHashIdentify()}",
]);
$emailService->setTemplateBody("recover_user");
$send = $emailService->send();
}
if($send){
$user->setRecoverAttempt($user->getRecoverAttempt() + 1);
}
$this->em->flush();
return $this->eadResponse([
"continue" => true,
"send" => $send,
"email" => $user->getEmail(),
"dateRecover" => date('Y-m-d'),
]);
}
/**
* @Route(
* path = "/admin/user/send/confirmation/{id}",
* name = "userConfirm",
* methods = {"GET"},
* requirements = { "id" = "\d+" }
* )
*/
public function sendUserConfirmation(Request $request) {
$permission = $this->userPermissionUtil->getPermission(
"user",
"send_confirmation"
);
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$send = null;
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"id" => $userId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if(!$user){
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$emailService = $this->generalService->getService('EmailService');
$password = "******";
if($user->getInvited() == UserEnum::YES){
$password = $this->stringUtil->randomText(6);
$user->setPassword($password);
$user->setOldPassword($user->getPassword());
}
if(
$emailService->checkUserToSend($user, false) ||
$this->configuration->get("allow_send_email_user") == UserEnum::YES
){
$emailService->setToEmail($user->getEmail());
$emailService->setToName($user->getName());
$subText = $this->configuration->getLanguage('user_confirmation.subject', 'email');
$subject = "{$subText} - {$this->client->getBrand()}";
$emailService->setSubject($subject);
$domain = $this->client->getDomainPrimary();
$emailService->setData([
"userName" => $user->getName(),
"userEmail" => $user->getEmail(),
"invited" => $user->getInvited(),
"userDateRegister" => $user->getDateRegister("d/m/Y"),
"userPassword" => $password,
"btnLink" => "https://{$domain}/confirm/{$user->getHashIdentify()}",
"isCart" => UserEnum::NO
]);
$emailService->setTemplateBody("user_confirmation");
$send = $emailService->send();
}
$this->em->flush();
return $this->eadResponse([ "send" => $send ]);
}
/**
* @Route(
* path = "/admin/user/login/{id}",
* name = "userLogin",
* methods = {"GET"},
* requirements = { "id" = "\d+" }
* )
*/
public function loginUser(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "login");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"id" => $userId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($user->getStatus() == UserEnum::INACTIVE){
return $this->eadResponse([
"message" => "User Inactive"
], ErrorEnum::ACTION_INVALID);
}
if($user->getStatus() == UserEnum::BLOCK){
return $this->eadResponse([
"message" => "User Blocked"
], ErrorEnum::ACTION_INVALID);
}
$maxAllowID = $this->user->getUserProfile()->getId();
$allowPermissions = [
UserEnum::STUDENT
];
$userProfileId = $user->getUserProfile()->getId();
if($maxAllowID == UserEnum::ADMIN && $userProfileId != UserEnum::ADMIN){
$allowPermissions[] = $userProfileId;
}else if($maxAllowID == UserEnum::TEACHER){
$allowPermissions[] = UserEnum::TUTOR;
}
if(!in_array($userProfileId, $allowPermissions) && $this->user->getId() != UserEnum::YES){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$session = new Session();
$ipApi = $this->generalService->getService('IpApiService');
$ipApi->setRequest($request);
$userAgent = $request->headers->get('User-Agent');
$session->setUser($user);
$session->setIp($ipApi->getIp());
$session->setIspName($ipApi->getIsp());
$session->setCoordinate($ipApi->getCoordinate());
$session->setTimeZone($ipApi->getTimeZone());
$session->setCity($ipApi->getCity());
$session->setState($ipApi->getState());
$session->setCountry($ipApi->getCountry());
$session->setIsAdmin(UserEnum::YES);
$session->setUserOrigin($this->user);
$session->setUserAgent($userAgent);
$errors = $this->validateEntity($session);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->configuration->alertLogin(
(string)$user->getEmail(),
(string)'LOGOU PELO ADMIN',
(string)$this->clientConfig->getId(),
(
in_array($request->getHost(), ConfigurationEnum::DOMAIN_DEV) ?
ConfigurationEnum::IP_LOCAL :
$request->getClientIp()
),
$userAgent,
$request->getHost(),
$user->getId(),
$this->user
);
//$this->sessionSym->set('session', $session);
//$this->em->getRepository(Cart::class)->updateCartHashToUser($user);
$time = time() + (24 * 60 * 60);
$this->generalService->setCookie('sessiontoken', $session->getToken(), $time);
$this->em->persist($session);
$this->em->flush();
$crmService = $this->generalService->getService('CRM\\CrmService');
$crmService->savePerson($user);
return $this->eadResponse([
"token" => $session->getToken(),
"userId" => $userId
]);
}
/**
* @Route(
* path = "/admin/user/block/{id}",
* name = "userBlock",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function blockUser(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "block");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"id" => $userId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$user->setStatus(UserEnum::BLOCK);
$sessions = $this->em->getRepository(Session::class)->findBy([
"user" => $user,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
foreach ($sessions as $key => $session) {
$session->delete();
}
$receivers = $this->em->getRepository(Receiver::class)->findBy([
"user" => $user,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
foreach ($receivers as $key => $receiver) {
$receiver->setStatus(ReceiverEnum::INACTIVE);
}
$errors = $this->validateEntity($user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
$this->repository->sendWebhook($user);
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/block/many",
* name = "userBlockMany",
* methods = {"PUT"}
* )
*/
public function blockUserMany(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "block");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$userId = $this->requestUtil->getField('ids');
if(empty($userId)){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
};
$userId = json_decode($userId);
foreach ($userId as $key => $id) {
$user = $this->repository->findOneBy([
"id" => $id,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($user) {
$user->setStatus(UserEnum::BLOCK);
$sessions = $this->em->getRepository(Session::class)->findBy([
"user" => $user,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
foreach ($sessions as $key => $session) {
$session->delete();
}
$receivers = $this->em->getRepository(Receiver::class)->findBy([
"user" => $user,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
foreach ($receivers as $key => $receiver) {
$receiver->setStatus(ReceiverEnum::INACTIVE);
}
$errors = $this->validateEntity($user);
if(!$errors){
$this->em->flush();
$this->repository->sendWebhook($user);
$data = $user->toReturn();
$this->userLogService->logUpdate(
"user",
$user->getId(),
$data
);
}
}
}
return $this->eadResponse([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/user/unblock/{id}",
* name = "userUnblock",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function unblockUser(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "unblock");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"id" => $userId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$userOldStatus = $user->getOldStatus();
$user->setStatus($userOldStatus);
$errors = $this->validateEntity($user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
$this->repository->sendWebhook($user);
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/unblock/many",
* name = "userUnblockMany",
* methods = {"PUT"}
* )
*/
public function unblockUserMany(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "unblock");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$userId = $this->requestUtil->getField('ids');
if(empty($userId)){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
};
$userId = json_decode($userId);
foreach ($userId as $key => $id) {
$user = $this->repository->findOneBy([
"id" => $id,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($user) {
$userOldStatus = $user->getOldStatus();
$user->setStatus($userOldStatus);
$errors = $this->validateEntity($user);
if(!$errors){
$this->em->flush();
$this->repository->sendWebhook($user);
$data = $user->toReturn();
$this->userLogService->logUpdate(
"user",
$user->getId(),
$data
);
}
}
}
return $this->eadResponse([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/user/activate/{id}",
* name = "userActivate",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function activateUser(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "enable");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"id" => $userId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($user->getStatus() == UserEnum::BLOCK){
return $this->eadResponse([
"message" => "Action invalid"
], ErrorEnum::ACTION_INVALID);
}
if($user->getStatus() == UserEnum::INACTIVE){
$userOldStatus = $user->getOldStatus();
$user->setStatus($userOldStatus);
}else{
$user->setStatus(UserEnum::ACTIVE);
}
$errors = $this->validateEntity($user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
$this->repository->sendWebhook($user);
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/activate/many",
* name = "userActivateMany",
* methods = {"PUT"}
* )
*/
public function activateUserMany(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "enable");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$userId = $this->requestUtil->getField('ids');
if(empty($userId)){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
};
$userId = json_decode($userId);
foreach ($userId as $key => $id) {
$user = $this->repository->findOneBy([
"id" => $id,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($user) {
if($user->getStatus() != UserEnum::BLOCK){
if($user->getStatus() == UserEnum::INACTIVE){
$userOldStatus = $user->getOldStatus();
$user->setStatus($userOldStatus);
}else{
$user->setStatus(UserEnum::ACTIVE);
}
}
$errors = $this->validateEntity($user);
if(!$errors){
$this->em->flush();
$this->repository->sendWebhook($user);
$data = $user->toReturn();
$this->userLogService->logUpdate(
"user",
$user->getId(),
$data
);
}
}
}
return $this->eadResponse([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/user/disable/{id}",
* name = "userDisable",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function disableUser(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "disable");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$userId = $request->get('id');
$user = $this->repository->findOneBy([
"id" => $userId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if (!$user) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
if($user->getStatus() == UserEnum::BLOCK){
return $this->eadResponse([
"message" => "Action invalid"
], ErrorEnum::ACTION_INVALID);
}
$user->setStatus(UserEnum::INACTIVE);
$errors = $this->validateEntity($user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
$this->repository->sendWebhook($user);
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/user/disable/many",
* name = "userDisableMany",
* methods = {"PUT"}
* )
*/
public function disableUserMany(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "disable");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$userId = $this->requestUtil->getField('ids');
if(empty($userId)){
return $this->eadResponse(null, ErrorEnum::ACTION_INVALID);
};
$userId = json_decode($userId);
foreach ($userId as $key => $id) {
$user = $this->repository->findOneBy([
"id" => $id,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($user) {
if($user->getStatus() != UserEnum::BLOCK){
$user->setStatus(UserEnum::INACTIVE);
}
$errors = $this->validateEntity($user);
if(!$errors){
$this->em->flush();
$this->repository->sendWebhook($user);
$data = $user->toReturn();
$this->userLogService->logUpdate(
"user",
$user->getId(),
$data
);
}
}
}
return $this->eadResponse([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/user/import",
* name = "userImport",
* methods = {"POST"},
* )
*/
public function importUser(Request $request) {
$permission = $this->userPermissionUtil->getPermission("user", "import");
if(!$this->userPermissionUtil->isHigh($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$file = $this->requestUtil->getFile('file');
if(empty($file)){
return $this->eadResponse([ "file" ], ErrorEnum::FIELD_EMPTY);
}
$spreadSheetUtil = $this->generalService->getUtil('SpreadSheetUtil');
$data = $spreadSheetUtil->import($file);
$groupsValidation = [];
$userProfileRepository = $this->em->getRepository(UserProfile::class);
$groupRepository = $this->em->getRepository(Group::class);
$groupId = $this->requestUtil->getField('group');
$groupRepository = $this->em->getRepository(Group::class);
$group = null;
if($groupId > 0){
$group = $groupRepository->findOneBy([
"id" => $groupId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
}
$allowPermissions = [
UserEnum::STUDENT
];
$permission = $this->userPermissionUtil->getPermission("user", "permissions");
if($this->userPermissionUtil->isHigh($permission)){
$maxAllowID = $this->user->getUserProfile()->getId();
$allowPermissions[] = $maxAllowID;
if($maxAllowID == UserEnum::ADMIN){
$allowPermissions[] = UserEnum::TUTOR;
$allowPermissions[] = UserEnum::TEACHER;
}else if($maxAllowID == UserEnum::TEACHER){
$allowPermissions[] = UserEnum::TUTOR;
}
}
foreach ($data as $key => $value) {
$name = trim($value['A']);
$document = !empty($value['G']) ? trim($value['G']) : null;
$email = trim($value['B']);
$password = trim($value['F']);
$custom = !empty($value['C']) ? trim($value['C']) : null;
$notes = !empty($value['D']) ? trim($value['D']) : null;
$userProfileId = !empty($value['E']) ? (int)$value['E'] : null;
if(!in_array($userProfileId, $allowPermissions)){
$userProfileId = null;
}
if(!empty($name) && !empty($email)){
$dataUser = [
"name" => $name,
"document" => $document,
"email" => $email,
"password" => $password,
"custom" => $custom,
"notes" => $notes,
"userProfileId" => $userProfileId,
"groupId" => $groupId,
"notify" => UserEnum::YES,
"invited" => UserEnum::YES
];
$userDTO = new UserDTO($dataUser);
$user = $this->repository->findOneBy([
"email" => $email
], [ "id" => "DESC" ]);
if(!$user || $user->isDeleted()){
$data = $this->repository->newUser($userDTO);
if($data->user){
$user = $data->user;
}
}
if($group && $user){
$user->addGroup($group);
$groupRepository->enrollOneUser($group, $user);
$clientDomain = $this->configuration->getActiveDomain(true);
$subText = $this->configuration->getLanguage(
'user_in_group.subject1', 'email'
);
$subText2 = $this->configuration->getLanguage(
'user_in_group.subject2', 'email'
);
$emailTitle = $subText . $group->getName() . $subText2;
$emailService = $this->generalService->getService('EmailService');
if($emailService->checkUserToSend($user)){
$emailService->setToEmail($user->getEmail());
$emailService->setToName($user->getName());
$emailService->setSubject($emailTitle);
$emailService->setData([
"userName" => $user->getName(),
"btnLink" => "https:{$clientDomain}",
"groupName" => $group->getName()
]);
$emailService->setTemplateBody("user_in_group");
$emailService->send();
}
}
}
}
$this->em->flush();
return $this->eadResponse([ "message" => "Successfully imported!" ]);
}
}