|
536 | 536 | throw new Exception('User not found', 404);
|
537 | 537 | }
|
538 | 538 |
|
539 |
| - $email = \strtolower($email); |
| 539 | + $isAnonymousUser = is_null($user->getAttribute('email')) && is_null($user->getAttribute('password')); // Check if request is from an anonymous account for converting |
| 540 | + if (!$isAnonymousUser) { |
| 541 | + // Remove previous unique ID. |
| 542 | + } |
| 543 | + |
| 544 | + $email = \strtolower($email); |
| 545 | + |
540 | 546 | try {
|
541 | 547 | $user = $dbForInternal->updateDocument('users', $user->getId(), $user->setAttribute('email', $email));
|
542 | 548 | } catch(Duplicate $th) {
|
|
545 | 551 |
|
546 | 552 | $audits
|
547 | 553 | ->setParam('userId', $user->getId())
|
548 |
| - ->setParam('event', 'account.update.email') |
| 554 | + ->setParam('event', 'users.update.email') |
549 | 555 | ->setParam('resource', 'user/'.$user->getId())
|
550 | 556 | ;
|
551 | 557 |
|
|
567 | 573 | ->param('userId', '', new UID(), 'User unique ID.')
|
568 | 574 | ->param('name', '', new Text(128), 'User name. Max length: 128 chars.')
|
569 | 575 | ->inject('response')
|
570 |
| - ->inject('projectDB') |
| 576 | + ->inject('dbForInternal') |
571 | 577 | ->inject('audits')
|
572 |
| - ->action(function ($userId, $name, $response, $projectDB, $audits) { |
| 578 | + ->action(function ($userId, $name, $response, $dbForInternal, $audits) { |
573 | 579 | /** @var Appwrite\Utopia\Response $response */
|
574 |
| - /** @var Appwrite\Database\Database $projectDB */ |
| 580 | + /** @var Utopia\Database\Database $dbForInternal */ |
575 | 581 | /** @var Appwrite\Event\Event $audits */
|
576 |
| - |
577 |
| - $user = $projectDB->getDocument($userId); |
578 | 582 |
|
579 |
| - if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) { |
| 583 | + $user = $dbForInternal->getDocument('users', $userId); |
| 584 | + |
| 585 | + if ($user->isEmpty() || $user->getAttribute('deleted')) { |
580 | 586 | throw new Exception('User not found', 404);
|
581 | 587 | }
|
582 | 588 |
|
583 |
| - $user = $projectDB->updateDocument(\array_merge($user->getArrayCopy(), [ |
584 |
| - 'name' => $name, |
585 |
| - ])); |
586 |
| - |
587 |
| - if (false === $user) { |
588 |
| - throw new Exception('Failed saving user to DB', 500); |
589 |
| - } |
| 589 | + $user = $dbForInternal->updateDocument('users', $user->getId(), $user->setAttribute('name', $name)); |
590 | 590 |
|
591 | 591 | $audits
|
592 | 592 | ->setParam('userId', $user->getId())
|
593 | 593 | ->setParam('event', 'users.update.name')
|
594 |
| - ->setParam('resource', 'users/'.$user->getId()) |
| 594 | + ->setParam('resource', 'user/'.$user->getId()) |
595 | 595 | ;
|
596 | 596 |
|
597 | 597 | $response->dynamic($user, Response::MODEL_USER);
|
|
612 | 612 | ->param('userId', '', new UID(), 'User unique ID.')
|
613 | 613 | ->param('password', '', new Password(), 'New user password. Must be between 6 to 32 chars.')
|
614 | 614 | ->inject('response')
|
615 |
| - ->inject('projectDB') |
| 615 | + ->inject('dbForInternal') |
616 | 616 | ->inject('audits')
|
617 |
| - ->action(function ($userId, $password, $response, $projectDB, $audits) { |
| 617 | + ->action(function ($userId, $password, $response, $dbForInternal, $audits) { |
618 | 618 | /** @var Appwrite\Utopia\Response $response */
|
619 |
| - /** @var Appwrite\Database\Database $projectDB */ |
| 619 | + /** @var Utopia\Database\Database $dbForInternal */ |
620 | 620 | /** @var Appwrite\Event\Event $audits */
|
621 | 621 |
|
622 |
| - $user = $projectDB->getDocument($userId); |
| 622 | + $user = $dbForInternal->getDocument('users', $userId); |
623 | 623 |
|
624 |
| - if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) { |
| 624 | + if ($user->isEmpty() || $user->getAttribute('deleted')) { |
625 | 625 | throw new Exception('User not found', 404);
|
626 | 626 | }
|
627 | 627 |
|
628 |
| - $user = $projectDB->updateDocument(\array_merge($user->getArrayCopy(), [ |
629 |
| - 'password' => Auth::passwordHash($password), |
630 |
| - 'passwordUpdate' => \time(), |
631 |
| - ])); |
| 628 | + $user |
| 629 | + ->setAttribute('password', $password) |
| 630 | + ->setAttribute('passwordUpdate', \time()); |
632 | 631 |
|
633 |
| - if (false === $user) { |
634 |
| - throw new Exception('Failed saving user to DB', 500); |
635 |
| - } |
| 632 | + $user = $dbForInternal->updateDocument('users', $user->getId(), $user); |
636 | 633 |
|
637 | 634 | $audits
|
638 | 635 | ->setParam('userId', $user->getId())
|
639 | 636 | ->setParam('event', 'users.update.password')
|
640 |
| - ->setParam('resource', 'users/'.$user->getId()) |
641 |
| - ; |
642 |
| - |
643 |
| - $response->dynamic($user, Response::MODEL_USER); |
644 |
| - }); |
645 |
| - |
646 |
| -App::patch('/v1/users/:userId/email') |
647 |
| - ->desc('Update Email') |
648 |
| - ->groups(['api', 'users']) |
649 |
| - ->label('event', 'users.update.email') |
650 |
| - ->label('scope', 'users.write') |
651 |
| - ->label('sdk.auth', [APP_AUTH_TYPE_KEY]) |
652 |
| - ->label('sdk.namespace', 'users') |
653 |
| - ->label('sdk.method', 'updateEmail') |
654 |
| - ->label('sdk.description', '/docs/references/users/update-user-email.md') |
655 |
| - ->label('sdk.response.code', Response::STATUS_CODE_OK) |
656 |
| - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) |
657 |
| - ->label('sdk.response.model', Response::MODEL_USER) |
658 |
| - ->param('userId', '', new UID(), 'User unique ID.') |
659 |
| - ->param('email', '', new Email(), 'User email.') |
660 |
| - ->inject('response') |
661 |
| - ->inject('projectDB') |
662 |
| - ->inject('audits') |
663 |
| - ->action(function ($userId, $email, $response, $projectDB, $audits) { |
664 |
| - /** @var Appwrite\Utopia\Response $response */ |
665 |
| - /** @var Appwrite\Database\Database $projectDB */ |
666 |
| - /** @var Appwrite\Event\Event $audits */ |
667 |
| - |
668 |
| - $user = $projectDB->getDocument($userId); |
669 |
| - |
670 |
| - if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) { |
671 |
| - throw new Exception('User not found', 404); |
672 |
| - } |
673 |
| - |
674 |
| - $isAnonymousUser = is_null($user->getAttribute('email')) && is_null($user->getAttribute('password')); // Check if request is from an anonymous account for converting |
675 |
| - $email = \strtolower($email); |
676 |
| - $profile = $projectDB->getCollectionFirst([ // Get user by email address |
677 |
| - 'limit' => 1, |
678 |
| - 'filters' => [ |
679 |
| - '$collection='.Database::SYSTEM_COLLECTION_USERS, |
680 |
| - 'email='.$email, |
681 |
| - ], |
682 |
| - ]); |
683 |
| - |
684 |
| - if (!empty($profile)) { |
685 |
| - throw new Exception('User already registered', 400); |
686 |
| - } |
687 |
| - |
688 |
| - if (!$isAnonymousUser) { |
689 |
| - // Remove previous unique ID. |
690 |
| - $projectDB->deleteUniqueKey(\md5($user->getArrayCopy()['$collection'].':'.'email'.'='.$user->getAttribute('email'))); |
691 |
| - } |
692 |
| - |
693 |
| - $user = $projectDB->updateDocument(\array_merge($user->getArrayCopy(), [ |
694 |
| - 'email' => $email, |
695 |
| - ])); |
696 |
| - |
697 |
| - $projectDB->addUniqueKey(\md5($user['$collection'].':'.'email'.'='.$email)); |
698 |
| - |
699 |
| - if (false === $user) { |
700 |
| - throw new Exception('Failed saving user to DB', 500); |
701 |
| - } |
702 |
| - |
703 |
| - $audits |
704 |
| - ->setParam('userId', $user->getId()) |
705 |
| - ->setParam('event', 'account.update.email') |
706 |
| - ->setParam('resource', 'users/'.$user->getId()) |
| 637 | + ->setParam('resource', 'user/'.$user->getId()) |
707 | 638 | ;
|
708 | 639 |
|
709 | 640 | $response->dynamic($user, Response::MODEL_USER);
|
|
784 | 715 | $dbForInternal->deleteDocument('sessions', $session->getId());
|
785 | 716 |
|
786 | 717 | $user->setAttribute('sessions', $sessions);
|
787 |
| - |
| 718 | + |
788 | 719 | $events
|
789 | 720 | ->setParam('eventData', $response->output($user, Response::MODEL_USER))
|
790 | 721 | ;
|
|
0 commit comments