Skip to content

Commit

Permalink
Merge pull request #40 from ikuseiGmbH/issue-39
Browse files Browse the repository at this point in the history
Join spaces with join policy 2 (open) #39
  • Loading branch information
donni106 authored Jul 14, 2022
2 parents 88025a8 + 39a9d75 commit 1aa8879
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public static function onRestApiAddRules()
//space
['pattern' => 'space', 'route' => 'smartVillage/space/space/find', 'verb' => 'GET'],
['pattern' => 'space/<spaceId:\d+>', 'route' => 'smartVillage/space/space/view', 'verb' => 'GET'],

//space membership
['pattern' => 'space/<spaceId:\d+>/membership/<userId:\d+>', 'route' => 'smartVillage/space/membership/create', 'verb' => 'POST'],
], 'smartVillage');
}
}
43 changes: 43 additions & 0 deletions controllers/space/MembershipController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace humhub\modules\smartVillage\controllers\space;

use humhub\modules\rest\components\BaseController;
use humhub\modules\space\models\Membership;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
use Yii;

class MembershipController extends BaseController
{
/**
* $spaceId = space id of that space you want adding member in it
* $userId = user id
* @param $spaceId
* @param $userId
* @return mixed
* Adding members without admin and space's owner.
*/
public function actionCreate($spaceId,$userId){
$space = Space::findOne(['id' => (int)$spaceId]);
$user = User::findOne(['id' => (int)$userId]);

if($space == null){
return $this->returnError(404,"Space not found");
}
if($user == null){
return $this->returnError(404,"User not found");
}

//check user is already a member of that space or not
$checkMembership = Membership::find()
->where(['space_id'=>$spaceId, 'user_id'=> $userId, 'status'=>Membership::STATUS_MEMBER])
->one();
if($checkMembership == null){
$space->addMember($userId, Yii::$app->request->get('canLeave', true), Yii::$app->request->get('silent', false));
return $this->returnSuccess('Member added!',200);
}else{
return $this->returnError(400,"User is already member of that space!");
}
}
}
36 changes: 34 additions & 2 deletions docs/postman/Smart Village API.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "626b3ce6-da9a-4c15-93ee-ab6f3d2306c8",
"_postman_id": "6083c583-3c4d-4eda-91b9-5fec0343fe5b",
"name": "Smart Village API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
Expand Down Expand Up @@ -275,6 +275,38 @@
}
},
"response": []
},
{
"name": "Membership-Add",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE2NTc2Mjg2NzAsImlzcyI6Imh0dHA6XC9cL2xvY2FsaG9zdFwvaHVtaHViIiwibmJmIjoxNjU3NjI4NjcwLCJ1aWQiOjMxLCJlbWFpbCI6ImdhdXJhdi5oYW5keXNvbHZlckBnbWFpbC5jb20ifQ.0Yb9w7LhMCXoUzX5uxK8V1UqH3TdGviE3buNzrLv-jLSM7k2MiBzeH_COapAHtEKVPTqQD5ItHOUqEcGw2Fx4Q",
"type": "string"
}
]
},
"method": "POST",
"header": [],
"url": {
"raw": "{{API_URL}}/api/v2/space/2/membership/31",
"host": [
"{{API_URL}}"
],
"path": [
"api",
"v2",
"space",
"2",
"membership",
"31"
]
}
},
"response": []
}
]
},
Expand Down Expand Up @@ -428,4 +460,4 @@
"response": []
}
]
}
}

0 comments on commit 1aa8879

Please sign in to comment.