Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Features Support For Login And Friendship Actions #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public static function onBeforeRequest($event)
['pattern' => 'api/v1/user/<id:\d+>', 'route' => 'rest/user/user/delete', 'verb' => ['DELETE']],
['pattern' => 'api/v1/user/full/<id:\d+>', '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/<id:\d+>', 'route' => 'rest/friendship/friendship/getrequests', 'verb' => 'GET'],
['pattern' => 'api/v1/friendship/getsentrequests/<id:\d+>', 'route' => 'rest/friendship/friendship/getsentrequests', 'verb' => 'GET'],

// User: Invite Controller
//['pattern' => 'api/v1/user/invite', 'route' => 'api/user/invite/index', 'verb' => 'POST'],
Expand Down
198 changes: 198 additions & 0 deletions controllers/friendship/FriendshipController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2018 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
* @author Usama Ayaz <[email protected]>
*/

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!');
}

}
58 changes: 58 additions & 0 deletions controllers/user/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"));


}



}
57 changes: 57 additions & 0 deletions definitions/FriendshipDefinitions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2018 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
* @author Usama Ayaz <[email protected]>
*/

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),


];
}


}