Implement super basic user sharing for budgets

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2020-03-21 14:09:14 -06:00
parent 3c4a5bd321
commit e4a3855d83
4 changed files with 258 additions and 211 deletions

View file

@ -16,173 +16,181 @@ use OCP\AppFramework\Http;
class BudgetController extends Controller
{
private $userId;
private $budgetMapper;
private $categoryMapper;
private $transactionMapper;
private $userPermissionMapper;
private $logger;
private $userId;
private $budgetMapper;
private $categoryMapper;
private $transactionMapper;
private $userPermissionMapper;
private $logger;
public function __construct(
$AppName,
IRequest $request,
ILogger $logger,
BudgetMapper $budgetMapper,
CategoryMapper $categoryMapper,
TransactionMapper $transactionMapper,
UserPermissionMapper $userPermissionMapper,
$UserId
) {
parent::__construct($AppName, $request);
$this->logger = $logger;
$this->userId = $UserId;
$this->budgetMapper = $budgetMapper;
$this->categoryMapper = $categoryMapper;
$this->transactionMapper = $transactionMapper;
$this->userPermissionMapper = $userPermissionMapper;
}
public function __construct(
$AppName,
IRequest $request,
ILogger $logger,
BudgetMapper $budgetMapper,
CategoryMapper $categoryMapper,
TransactionMapper $transactionMapper,
UserPermissionMapper $userPermissionMapper,
$UserId
) {
parent::__construct($AppName, $request);
$this->logger = $logger;
$this->userId = $UserId;
$this->budgetMapper = $budgetMapper;
$this->categoryMapper = $categoryMapper;
$this->transactionMapper = $transactionMapper;
$this->userPermissionMapper = $userPermissionMapper;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index()
{
$userPermissions = $this->userPermissionMapper->findAll($this->userId);
$budgets = [];
foreach ($userPermissions as $userPermission) {
array_push($budgets, $userPermission->getBudgetId());
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index()
{
$userPermissions = $this->userPermissionMapper->findAll($this->userId);
$budgets = [];
foreach ($userPermissions as $userPermission) {
array_push($budgets, $userPermission->getBudgetId());
}
return new DataResponse($this->budgetMapper->findAll($budgets));
}
return new DataResponse($this->budgetMapper->findAll($budgets));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id
*/
public function show(int $id)
{
try {
$userPermission = $this->userPermissionMapper->find($id, $this->userId);
return new DataResponse($this->budgetMapper->find($userPermission->getBudgetId()));
} catch (Exception $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id
*/
public function show(int $id)
{
try {
$userPermission = $this->userPermissionMapper->find($id, $this->userId);
return new DataResponse($this->budgetMapper->find($userPermission->getBudgetId()));
} catch (Exception $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $name
* @param ?string $description
* @param array $users
*/
public function create(string $name, ?string $description, array $users)
{
$budget = new Budget();
$budget->setName($name);
$budget->setDescription($description);
$budget = $this->budgetMapper->insert($budget);
$userPermissions = [];
$users[$this->userId] = UserPermission::PERMISSION_MANAGE;
foreach ($users as $user => $permission) {
$userPermission = new UserPermission();
$userPermission->setBudgetId($budget->getId());
$userPermission->setUserId($user);
$userPermission->setPermission($permission);
$userPermission = $this->userPermissionMapper->insert($userPermission);
array_push($userPermissions, $userPermission);
}
$budget->setUsers($userPermissions);
return new DataResponse($budget);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $name
* @param ?string $description
* @param array $users
*/
public function create(string $name, ?string $description, array $users)
{
$budget = new Budget();
$budget->setName($name);
$budget->setDescription($description);
$budget = $this->budgetMapper->insert($budget);
$userPermissions = [];
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id
* @param string $name
* @param string $description
* @param array $users
*/
public function update(int $id, string $name, string $description, array $users)
{
try {
$userPermission = $this->userPermissionMapper->find($id, $this->userId);
$budget = $this->budgetMapper->find($userPermission->getBudgetId());
} catch (Exception $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
if ($userPermission->getPermission() != UserPermission::PERMISSION_MANAGE) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}
if ($name) {
$budget->setName($name);
}
if ($description) {
$budget->setDescription($description);
}
$budget = $this->budgetMapper->update($budget);
if ($users) {
$this->userPermissionMapper->deleteAll($budget->id);
$userPermissions = [];
$users[$this->userId] = UserPermission::PERMISSION_MANAGE;
foreach ($users as $user => $permission) {
$userPermission = new UserPermission();
$userPermission->setBudgetId($budget->getId());
$userPermission->setUserId($user);
$userPermission->setPermission($permission);
array_push($userPermissions, $this->userPermissionMapper->insert($userPermission));
}
$budget->setUsers($userPermissions);
} else {
$budget->setUsers($userPermissionMapper->findAllByBudgetId($budget->getId()));
}
foreach ($users as $user => $permission) {
$this->logger->error("User: $user Permission: $permission");
}
return new DataResponse($budget);
}
foreach ($users as $user) {
if ($user['user'] === $this->userId) {
continue;
}
$userPermission = new UserPermission();
$userPermission->setBudgetId($budget->getId());
$userPermission->setUserId($user['user']);
$userPermission->setPermission($user['permission']);
$userPermission = $this->userPermissionMapper->insert($userPermission);
array_push($userPermissions, $userPermission);
}
$userPermission = new UserPermission();
$userPermission->setBudgetId($budget->getId());
$userPermission->setUserId($this->userId);
$userPermission->setPermission(UserPermission::PERMISSION_MANAGE);
array_push($userPermissions, $this->userPermissionMapper->insert($userPermission));
$budget->setUsers($userPermissions);
return new DataResponse($budget);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id
*/
public function destroy(int $id)
{
try {
$userPermission = $this->userPermissionMapper->find($id, $this->userId);
$budget = $this->budgetMapper->find($userPermission->getBudgetId());
} catch (Exception $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
if ($userPermission->getPermission() != UserPermission::PERMISSION_MANAGE) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}
// Delete all user permissions for this budget
$this->userPermissionMapper->deleteAll($budget->getId());
// Delete all transactions for this budget
$this->transactionMapper->deleteAll($budget->getId());
// Delete all categories for this budget
$this->categoryMapper->deleteAll($budget->getId());
// Finally, delete the budget itself
$this->budgetMapper->delete($budget);
return new DataResponse($budget);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id
* @param string $name
* @param string $description
* @param array $users
*/
public function update(int $id, string $name, string $description, array $users)
{
try {
$userPermission = $this->userPermissionMapper->find($id, $this->userId);
$budget = $this->budgetMapper->find($userPermission->getBudgetId());
} catch (Exception $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
if ($userPermission->getPermission() != UserPermission::PERMISSION_MANAGE) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}
$budget->setName($name);
$budget->setDescription($description);
$budget = $this->budgetMapper->update($budget);
if ($users) {
$this->userPermissionMapper->deleteAll($budget->id);
$userPermissions = [];
foreach ($users as $user) {
if ($user['user'] === $this->userId) {
continue;
}
$userPermission = new UserPermission();
$userPermission->setBudgetId($budget->getId());
$userPermission->setUserId($user['user']);
$userPermission->setPermission($user['permission']);
array_push($userPermissions, $this->userPermissionMapper->insert($userPermission));
}
$userPermission = new UserPermission();
$userPermission->setBudgetId($budget->getId());
$userPermission->setUserId($this->userId);
$userPermission->setPermission(UserPermission::PERMISSION_MANAGE);
array_push($userPermissions, $this->userPermissionMapper->insert($userPermission));
$budget->setUsers($userPermissions);
} else {
$budget->setUsers($userPermissionMapper->findAllByBudgetId($budget->getId()));
}
return new DataResponse($budget);
}
public function stats(int $budgetId) {
try {
$userPermission = $this->userPermissionMapper->find($id, $this->userId);
$budget = $this->budgetMapper->find($userPermission->getBudgetId());
} catch (Exception $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id
*/
public function destroy(int $id)
{
try {
$userPermission = $this->userPermissionMapper->find($id, $this->userId);
$budget = $this->budgetMapper->find($userPermission->getBudgetId());
} catch (Exception $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
if ($userPermission->getPermission() != UserPermission::PERMISSION_MANAGE) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}
// Delete all user permissions for this budget
$this->userPermissionMapper->deleteAll($budget->getId());
// Delete all transactions for this budget
$this->transactionMapper->deleteAll($budget->getId());
// Delete all categories for this budget
$this->categoryMapper->deleteAll($budget->getId());
// Finally, delete the budget itself
$this->budgetMapper->delete($budget);
return new DataResponse($budget);
}
public function stats(int $budgetId) {
try {
$userPermission = $this->userPermissionMapper->find($id, $this->userId);
$budget = $this->budgetMapper->find($userPermission->getBudgetId());
} catch (Exception $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
}
}

View file

@ -1,61 +1,85 @@
<template>
<div>
<div v-if="!loading" class="add-edit-budget">
<h2>{{ budget.id ? 'Edit' : 'Add' }} Budget</h2>
<input v-model="budget.name" type="text" placeholder="Name" title="Name" />
<textarea v-model="budget.description" placeholder="Description" title="Description"></textarea>
<button @click="saveBudget()">Save Budget</button>
<div>
<div v-if="!loading" class="add-edit-budget">
<h2>{{ budget.id ? 'Edit' : 'Add' }} Budget</h2>
<input v-model="budget.name" type="text" placeholder="Name" title="Name" />
<textarea v-model="budget.description" placeholder="Description" title="Description"></textarea>
<div class="sharing">
<h3>Sharing</h3>
<input v-model="user" v-on:keyup.enter="addPermission()" type="test" placeholde="User" title="User" />
<ul v-if="budget.users" class="sharing-users">
<li v-for="user in budget.users">
<span v-if="user.user">
{{ user.user }}
</span>
<span v-if="user.permission">
: {{ user.permission }}
</span>
</li>
</ul>
</div>
<button @click="saveBudget()">Save Budget</button>
</div>
<div v-if="loading" class="icon-loading"></div>
</div>
<div v-if="loading" class="icon-loading"></div>
</div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
name: "add-edit-budget",
components: {
},
data: function() {
return {
saving: false
};
},
props: {
budget: Object
},
computed: {
loading: state => state.budget === undefined || state.saving
},
methods: {
saveBudget() {
this.saving = true;
this.$store.dispatch("budgetFormSaveClicked", this.budget);
name: "add-edit-budget",
components: {
},
data: function() {
return {
saving: false,
user: undefined
};
},
props: {
budget: Object,
},
computed: {
loading: state => state.budget === undefined || state.saving
},
methods: {
addPermission() {
const user = this.user
this.user = undefined;
this.budget.users = this.budget.users.filter(u => u.user != user)
this.budget.users.push({
"user": user,
"permission": 2
})
},
saveBudget() {
this.saving = true;
this.$store.dispatch("budgetFormSaveClicked", this.budget);
}
},
mounted() {
let budgetId;
if (this.budget) {
budgetId = this.budget.id;
}
}
},
mounted() {
let budgetId;
if (this.budget) {
budgetId = this.budget.id;
}
}
};
</script>
<style scoped>
.add-edit-budget > * {
display: block;
width: 100%;
max-width: 500px;
display: block;
width: 100%;
max-width: 500px;
}
.radio-container {
display: flex;
align-items: center;
display: flex;
align-items: center;
}
.radio-container label {
margin-right: 1em;
margin-right: 1em;
}
.icon-loading {
margin-top: 16px;
margin-top: 16px;
}
</style>

View file

@ -1,18 +1,20 @@
<template>
<BudgetForm :budget="budget" />
<BudgetForm :budget="budget" />
</template>
<script>
import BudgetForm from "./BudgetForm";
export default {
name: "new-budget",
components: {
BudgetForm
},
data: function() {
return {
budget: {}
};
}
name: "new-budget",
components: {
BudgetForm
},
data: function() {
return {
budget: {
users: []
}
}
}
};
</script>

View file

@ -147,6 +147,14 @@ export default new Vuex.Store({
router.push({ name: "categoryDetails", params: { id: response.data.id } })
})
},
deleteCategoryClicked({ commit, state }, categoryId) {
axios.delete(OC.generateUrl(`/apps/twigs/api/v1.0/categories/${categoryId}`))
.then((response) => {
commit('setCurrentCategory', undefined)
commit('deleteCategory', response.data)
router.push({ name: "budgetDetails", params: { id: state.currentBudget } })
})
},
addTransactionClicked({ commit }) {
router.push({ name: "newTransaction" })
},
@ -250,6 +258,11 @@ export default new Vuex.Store({
[data.categoryId]: data.sum
}
},
deleteCategory(state, category) {
state.categories = [
...state.categories.filter(c => c.id !== category.id),
]
},
addTransaction(state, transaction) {
state.transactions = [
...state.transactions.filter(t => t.id !== transaction.id),