Skip to content

Commit

Permalink
Merge 0cc8643 into 4cc058d
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelStArnaud committed Oct 19, 2015
2 parents 4cc058d + 0cc8643 commit ed7c741
Show file tree
Hide file tree
Showing 24 changed files with 1,360 additions and 299 deletions.
28 changes: 28 additions & 0 deletions config/Migrations/20151014185330_create_organizations_owners.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
use Migrations\AbstractMigration;

class CreateOrganizationsOwners extends AbstractMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
* @return void
*/
public function change()
{
$table = $this->table('organizations_owners');
$table->addColumn('organization_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
]);
$table->addColumn('user_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
]);
$table->create();
}
}
28 changes: 28 additions & 0 deletions config/Migrations/20151014200927_create_organizations_members.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
use Migrations\AbstractMigration;

class CreateOrganizationsMembers extends AbstractMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
* @return void
*/
public function change()
{
$table = $this->table('organizations_members');
$table->addColumn('organization_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
]);
$table->addColumn('user_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
]);
$table->create();
}
}
197 changes: 193 additions & 4 deletions src/Controller/OrganizationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ class OrganizationsController extends AppController
'index' => ['Student', 'Mentor', 'Administrator'],
'editStatus' => ['Administrator'],
'add' => ['Administrator'],
'addMember' => ['Student'],
'addOwner' => ['Student'],
'submit' => ['Student', 'Mentor', 'Administrator'],
'edit' => ['Administrator'],
'editValidated' => ['Administrator'],
'editRejected' => ['Administrator'],
'view' => ['Student', 'Mentor', 'Administrator'],
'delete' => ['Administrator']
'delete' => ['Administrator'],
'quit' => ['Student']
];

/**
Expand All @@ -44,7 +47,7 @@ class OrganizationsController extends AppController
public function isAuthorized($user)
{
$user = $this->Users->findById($user['id'])->first();

if (isset($this->_permissions[$this->request->action])) {
if ($user->hasRoleName($this->_permissions[$this->request->action])) {
return true;
Expand Down Expand Up @@ -144,11 +147,18 @@ public function view($id = null)
$organization = $this->Organizations->get(
$id,
[
'contain' => ['Projects']
'contain' => ['Projects', 'Members', 'Owners']
]
);

$members = [];

$members = array_merge($organization->owners, $organization->members);
$members = array_unique($members);


$user = $this->Users->findById($this->request->session()->read('Auth.User.id'))->first();
$this->set(compact('organization', 'user'));
$this->set(compact('organization', 'user', 'members'));
$this->set('_serialize', ['organization']);
}

Expand All @@ -162,6 +172,9 @@ public function add()

$organization->editIsValidated(true);
$organization->editIsRejected(false);
$owner = $this->Organizations->Owners->findById($this->request->session()->read('Auth.User.id'))->first();
$organization->owners = [$owner];
$organization->members = [$owner];

if ($this->request->is('post')) {
$organization = $this->Organizations->patchEntity($organization, $this->request->data);
Expand All @@ -186,6 +199,9 @@ public function submit()

$organization->editIsValidated(false);
$organization->editIsRejected(false);
$owner = $this->Organizations->Owners->findById($this->request->session()->read('Auth.User.id'))->first();
$organization->owners = [$owner];
$organization->members = [$owner];

if ($this->request->is('post')) {
$organization = $this->Organizations->patchEntity($organization, $this->request->data);
Expand Down Expand Up @@ -305,4 +321,177 @@ public function delete($id = null)
}
return $this->redirect(['action' => 'index']);
}

/**
* Add member to the organization
*
* @param int $id organization id
*
* @return void
*/
public function addMember($id = null)
{
$organization = $this->Organizations->get(
$id,
[
'contain' => ['Members', 'Owners']
]
);

$users = $this->loadModel("Users")->find('all')->toArray();
$members = $organization->members;
$you = $this->request->session()->read('Auth.User.id');

if (!$organization->isOwner($you)) {
return $this->redirect(['action' => 'view', $organization->id]);
}

if ($this->request->is(['patch', 'post', 'put'])) {
$usersSelected = [];

if (count($this->request->data)) {
foreach ($this->request->data['users'] as $id) {
$user = $this->Users->get($id);
array_push($usersSelected, $user);
}
}

$user = $this->Users->get($you);
array_push($usersSelected, $user);

$organization->members = $usersSelected;

if ($this->Organizations->save($organization)) {
$this->Flash->success(__('The user has been added.'));
return $this->redirect(['action' => 'view', $organization->id]);
} else {
$this->Flash->error(
__('The user could not be added. Please,try again.')
);
}
}

$this->set(compact('organization', 'users', 'members', 'you'));
$this->set('_serialize', ['organization']);
}

/**
* Add owner to the organization
*
* @param int $id organization id
*
* @return void
*/
public function addOwner($id = null)
{
$organization = $this->Organizations->get(
$id,
[
'contain' => ['Owners', 'Members']
]
);

$users = $this->loadModel("Users")->find('all')->toArray();
$owners = $organization->owners;
$you = $this->request->session()->read('Auth.User.id');

if (!$organization->isOwner($this->request->session()->read('Auth.User.id'))) {
return $this->redirect(['action' => 'view', $organization->id]);
}

if ($this->request->is(['patch', 'post', 'put'])) {
$usersSelected = [];
$members = $organization->members;

if (count($this->request->data)) {
foreach ($this->request->data['users'] as $id) {
$user = $this->Users->get($id);
array_push($usersSelected, $user);
array_push($members, $user);
}
}

$user = $this->Users->get($you);
array_push($usersSelected, $user);

$organization->owners = $usersSelected;

$user = $this->Users->get($you);
array_push($members, $user);

$members = array_unique($members);

$organization->members = $members;

if ($this->Organizations->save($organization)) {
$this->Flash->success(__('The user has been added.'));
return $this->redirect(['action' => 'view', $organization->id]);
} else {
$this->Flash->error(
__('The user could not be added. Please,try again.')
);
}

}

$this->set(compact('organization', 'users', 'owners', 'you'));
$this->set('_serialize', ['organization']);
}

/**
* Quit method
* @param string $id id
* @return redirect
*/
public function quit($id = null)
{
$organization = $this->Organizations->get(
$id,
[
'contain' => ['Owners', 'Members']
]
);

$users = $this->loadModel("Users")->find('all')->toArray();
$owners = $organization->owners;
$members = $organization->members;
$you = $this->request->session()->read('Auth.User.id');

if (!$organization->isOwner($you) || !$organization->isMember($you)) {
return $this->redirect(['action' => 'view', $organization->id]);
}

if ($this->request->is(['patch', 'post', 'put'])) {
$nbMembers = count($members);
for ($i = 0; $i < $nbMembers; $i++) {
if ($members[$i]->getId() == $you) {
unset($members[$i]);
}
}

$nbOwners = count($owners);
for ($i = 0; $i < $nbOwners; $i++) {
if ($owners[$i]->getId() == $you) {
unset($owners[$i]);
}
}

$organization->members = $members;
$organization->owners = $owners;

if (count($owners)) {
if ($this->Organizations->save($organization)) {
$this->Flash->success(__('You have left the organization.'));
return $this->redirect(['action' => 'view', $organization->id]);
} else {
$this->Flash->error(__('There was an error. Please,try again.'));
}
} else {
$this->Flash->error(__('You are the only owner, contact an administrator.'));
}
}

$this->set(compact('organization', 'you'));
$this->set('_serialize', ['organization']);
}
}
2 changes: 1 addition & 1 deletion src/Locale/cake.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"POT-Creation-Date: 2015-10-08 14:33+0000\n"
"POT-Creation-Date: 2015-10-19 18:29+0000\n"
"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\n"
"Last-Translator: NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <EMAIL@ADDRESS>\n"
Expand Down
Loading

0 comments on commit ed7c741

Please sign in to comment.