From 4b456c54f482b21b2d9c9335e113dc11f33f2d1a Mon Sep 17 00:00:00 2001 From: Usama Ayaz Date: Wed, 19 Jun 2019 19:09:43 +0500 Subject: [PATCH] Enhance Features Support For Login And Friendship actions - I have added the User Login API - I have also added the friendship send, accept, list API(s) --- Events.php | 8 + .../friendship/FriendshipController.php | 198 ++++++++++++++++++ controllers/user/UserController.php | 58 +++++ definitions/FriendshipDefinitions.php | 57 +++++ 4 files changed, 321 insertions(+) create mode 100644 controllers/friendship/FriendshipController.php create mode 100644 definitions/FriendshipDefinitions.php diff --git a/Events.php b/Events.php index 2fcabe8..ce7114c 100644 --- a/Events.php +++ b/Events.php @@ -34,6 +34,14 @@ public static function onBeforeRequest($event) ['pattern' => 'api/v1/user/', 'route' => 'rest/user/user/delete', 'verb' => ['DELETE']], ['pattern' => 'api/v1/user/full/', 'route' => 'rest/user/user/hard-delete', 'verb' => ['DELETE']], ['pattern' => 'api/v1/user/', 'route' => 'rest/user/user/create', 'verb' => 'POST'], + ['pattern' => 'api/v1/user/auth', 'route' => 'rest/user/user/login', 'verb' => 'POST'], + + + // Friendship + ['pattern' => 'api/v1/friendship/send-request', 'route' => 'rest/friendship/friendship/sendrequest', 'verb' => 'POST'], + ['pattern' => 'api/v1/friendship/accept-request', 'route' => 'rest/friendship/friendship/acceptrequest', 'verb' => 'POST'], + ['pattern' => 'api/v1/friendship/getrequests/', 'route' => 'rest/friendship/friendship/getrequests', 'verb' => 'GET'], + ['pattern' => 'api/v1/friendship/getsentrequests/', 'route' => 'rest/friendship/friendship/getsentrequests', 'verb' => 'GET'], // User: Invite Controller //['pattern' => 'api/v1/user/invite', 'route' => 'api/user/invite/index', 'verb' => 'POST'], diff --git a/controllers/friendship/FriendshipController.php b/controllers/friendship/FriendshipController.php new file mode 100644 index 0000000..2765400 --- /dev/null +++ b/controllers/friendship/FriendshipController.php @@ -0,0 +1,198 @@ + + */ + +namespace humhub\modules\rest\controllers\friendship; + +use humhub\modules\rest\components\BaseController; +use humhub\modules\rest\definitions\FriendshipDefinitions; +use humhub\modules\friendship\models\Friendship; +use humhub\modules\user\models\User; +use Yii; +use yii\web\HttpException; + +/** + * Membership Handling Controller + * + * @property Module $module + * @author luke + */ +class FriendshipController extends BaseController { + + /** + * @inheritdoc + * @throws HttpException + */ + public function actionSendrequest() { + + + $friend = User::findOne(['id' => Yii::$app->request->post('friendId')]); + + if ($friend === null) { + throw new HttpException(404, 'Friend User not found!'); + } + + $user = User::findOne(['id' => Yii::$app->request->post('userId')]); + + if ($user === null) { + throw new HttpException(404, 'User not found!'); + } + + if ($user->id === $friend->id) { + throw new HttpException(404, 'You cannot send request to yourself!'); + } + + $check_friendship = Friendship::findOne(['user_id' => $friend->id, 'friend_user_id' => $user->id]); + $check_request = Friendship::findOne(['user_id' => $user->id, 'friend_user_id' => $friend->id]); + + if ($check_request !== NULL && $check_friendship !== NULL) { + throw new HttpException(404, 'Users are already friends!'); + } + + if ($check_request !== NULL) { + throw new HttpException(404, 'Friend request already sent!'); + } + + if ($check_friendship !== NULL) { + throw new HttpException(404, 'User has already sent request to you!'); + } + + $friendship = new Friendship(); + $friendship->user_id = $user->id; + $friendship->friend_user_id = $friend->id; + + if ($friendship->hasErrors()) { + return $this->returnError(400, 'Validation failed', [ + 'friendship' => $friendship->getErrors(), + ]); + } + + if ($friendship->save()) { + return $this->actionView($friendship->id); + } + + Yii::error('Could not send request.', 'api'); + return $this->returnError(500, 'Internal error while sending friend request!'); + } + + public function actionView($id) { + + $friendship = Friendship::findOne(['id' => $id]); + if ($friendship === null) { + return $friendship->returnError(404, 'Friendship not found!'); + } + + return FriendshipDefinitions::getFriendship($friendship); + } + + public function actionAcceptrequest() { + + $friend = User::findOne(['id' => Yii::$app->request->post('friendId')]); + + if ($friend === null) { + throw new HttpException(404, 'Friend User not found!'); + } + + $user = User::findOne(['id' => Yii::$app->request->post('userId')]); + + if ($user === null) { + throw new HttpException(404, 'User not found!'); + } + + if ($user->id === $friend->id) { + throw new HttpException(404, 'You cannot accept your own request!'); + } + + $check_friendship = Friendship::findOne(['user_id' => $friend->id, 'friend_user_id' => $user->id]); + $check_request = Friendship::findOne(['user_id' => $user->id, 'friend_user_id' => $friend->id]); + + if ($check_friendship === NULL) { + throw new HttpException(404, 'User has not sent request to you!'); + } + + if ($check_request !== NULL && $check_friendship !== NULL) { + throw new HttpException(404, 'Users are already friends!'); + } + +// if ($check_request !== NULL) { +// throw new HttpException(404, 'Friend request already sent!'); +// } + + + $friendship = new Friendship(); + $friendship->user_id = $user->id; + $friendship->friend_user_id = $friend->id; + + if ($friendship->hasErrors()) { + return $this->returnError(400, 'Validation failed', [ + 'friendship' => $friendship->getErrors(), + ]); + } + + if ($friendship->save()) { + return $this->actionView($friendship->id); + } + + Yii::error('Could not accept request.', 'api'); + return $this->returnError(500, 'Internal error while accepting friend request!'); + } + + public function actionGetrequests($id) { + $user = User::findOne(['id' => $id]); + + if ($user === null) { + throw new HttpException(404, 'User not found!'); + } + + if ($user !== null) { + $results = []; + $user_requests = Friendship::findBySql('SELECT snd.* FROM user ufr' + . ' LEFT JOIN user_friendship snd ON ufr.id=snd.user_id AND snd.friend_user_id=' . $user->id . '' + . ' LEFT JOIN user_friendship recv ON ufr.id=recv.friend_user_id AND recv.user_id=' . $user->id . '' + . ' WHERE recv.id IS NULL AND snd.id IS NOT NULL' + ); + + foreach ($user_requests->all() as $request) { + $results[] = FriendshipDefinitions::getFriendShipForSend($request); + } + + + return $results; + } + + Yii::error('Could not accept request.', 'api'); + return $this->returnError(500, 'Internal error while accepting friend request!'); + } + + public function actionGetsentrequests($id) { + $user = User::findOne(['id' => $id]); + + if ($user === null) { + throw new HttpException(404, 'User not found!'); + } + + if ($user !== null) { + $results = []; + $user_requests = Friendship::findBySql('SELECT recv.* FROM user ufr' + . ' LEFT JOIN user_friendship snd ON ufr.id=snd.user_id AND snd.friend_user_id=' . $user->id . '' + . ' LEFT JOIN user_friendship recv ON ufr.id=recv.friend_user_id AND recv.user_id=' . $user->id . '' + . ' WHERE recv.id IS NOT NULL AND snd.id IS NULL' + ); + foreach ($user_requests->all() as $request) { + $results[] = FriendshipDefinitions::getFriendShipForReceive($request); + } + + + return $results; + } + + Yii::error('Could not accept request.', 'api'); + return $this->returnError(500, 'Internal error while accepting friend request!'); + } + +} diff --git a/controllers/user/UserController.php b/controllers/user/UserController.php index 692a9ac..8fb5017 100644 --- a/controllers/user/UserController.php +++ b/controllers/user/UserController.php @@ -9,12 +9,19 @@ use humhub\modules\rest\components\BaseController; use humhub\modules\rest\definitions\UserDefinitions; +use humhub\modules\user\models\forms\Login; use humhub\modules\user\models\Password; use humhub\modules\user\models\Profile; use humhub\modules\user\models\User; use Yii; use yii\web\HttpException; +use humhub\modules\user\authclient\AuthClientHelpers; +use humhub\modules\user\authclient\interfaces\ApprovalBypass; +use humhub\modules\user\authclient\BaseFormAuth; +use humhub\modules\user\authclient\AuthAction; +use yii\authclient\BaseClient; + /** * Class AccountController @@ -182,6 +189,57 @@ public function actionHardDelete($id) return $this->returnError(500, 'Internal error while soft delete user!'); } + + public function actionLogin(){ + + // Login Form Handling + $login = new Login; + + if ($login->load(Yii::$app->request->post()) && $login->validate()) { + return $this->onAuthSuccess($login->authClient); + }else{ + return $this->returnError(400, Yii::t('UserModule.base', 'User validation failed.')); + } + + return $this->returnError(500, 'Internal error while save user!'); + } + + public function onAuthSuccess(BaseClient $authClient) + { + + $attributes = $authClient->getUserAttributes(); + + + // Login existing user + $user = AuthClientHelpers::getUserByAuthClient($authClient); + + if ($user !== null) { + return $this->actionView($user->id); + } + + if (!$authClient instanceof ApprovalBypass && !Yii::$app->getModule('user')->settings->get('auth.anonymousRegistration')) { + return $this->returnError(404, Yii::t('UserModule.base', "You're not registered.")); + } + + // Check if E-Mail is given + if (!isset($attributes['email']) && Yii::$app->getModule('user')->emailRequired) { + return $this->returnError(400, Yii::t('UserModule.base', 'Missing E-Mail Attribute from AuthClient.')); + } + + if (!isset($attributes['id'])) { + return $this->returnError(400, Yii::t('UserModule.base', 'Missing ID AuthClient Attribute from AuthClient.')); + } + + // Check if e-mail is already taken + if (isset($attributes['email']) && User::findOne(['email' => $attributes['email']]) !== null) { + return $this->returnError(400, Yii::t('UserModule.base', 'User with the same email already exists but isn\'t linked to you. Login using your email first to link it.')); + } + + return $this->returnError(400, Yii::t('UserModule.base', "Please check your data it is a bad request")); + + + } + } \ No newline at end of file diff --git a/definitions/FriendshipDefinitions.php b/definitions/FriendshipDefinitions.php new file mode 100644 index 0000000..9e1d8f7 --- /dev/null +++ b/definitions/FriendshipDefinitions.php @@ -0,0 +1,57 @@ + + */ + +namespace humhub\modules\rest\definitions; + +use humhub\modules\friendship\models\Friendship; +use humhub\modules\user\models\User; +use yii\helpers\Url; + +/** + * Class AccountController + */ +class FriendshipDefinitions { + + + public static function getFriendship(Friendship $friendship) { + + return [ + 'id' => $friendship->id, + 'created_at' => $friendship->created_at, + 'friend' => UserDefinitions::getUserShort($friendship->friendUser), + 'user' => UserDefinitions::getUserShort($friendship->user) + + ]; + } + + + public static function getFriendShipForSend(Friendship $friendship) { + + return [ + 'id' => $friendship->id, + 'created_at' => $friendship->created_at, + 'friend' => UserDefinitions::getUserShort($friendship->user), + + + ]; + } + + public static function getFriendShipForReceive(Friendship $friendship) { + + return [ + 'id' => $friendship->id, + 'created_at' => $friendship->created_at, + 'friend' => UserDefinitions::getUserShort($friendship->friendUser), + + + ]; + } + + +}