Skip to content

Commit

Permalink
implement accessgrant
Browse files Browse the repository at this point in the history
  • Loading branch information
angelol committed Jan 31, 2019
1 parent 908c216 commit 555c2a7
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/frontend/node_modules
/contract/squeakr.wasm
/contract/squeakr.abi
.DS_Store
4 changes: 4 additions & 0 deletions contract/squeakr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ void squeakr::accessgrant(const name user, const name contract, const std::strin
const auto followers_idx = followers.template get_index<"combined"_n>();
const auto followers_itr = followers_idx.find(combine_ids(user.value, giver.value));
check(followers_itr != followers_idx.end(), "You are not following this user, permission denied");

followers.modify(followers.find(followers_itr->id), same_payer, [&](auto& x) {
x.access_granted = true;
});
}

ACTION squeakr::admclear(const name sender) {
Expand Down
26 changes: 23 additions & 3 deletions frontend/src/Backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ class Backend {
}

async accept(follower) {
const {priveos, nonce, key} = await this.getPriveos()

const actions = [{
account: contract,
name: 'accept',
Expand Down Expand Up @@ -254,14 +252,15 @@ class Backend {
return res.rows.filter(x => x.followee == this.account.name)
}

async followRequests(user) {
async followRequests() {
const res = await this.eos_api.getTableRows({
json: true,
code: contract,
scope: contract,
table: 'request',
limit: 100,
})
console.log("inside followRequests: res.rows: ", res.rows)
return res.rows.filter(x => x.followee == this.account.name)
}

Expand All @@ -281,7 +280,28 @@ class Backend {
const res = await this.eos.transaction({actions})
console.log(res)
}

async requestAccess(user) {
const { priveos } = await this.getPriveos()
await priveos.accessgrant(this.account.name, user, "4,EOS")
const {key, nonce} = await priveos.read(this.account.name, user)
addKey(user, key, nonce)
}
}

/**
* In a production application, it would be better to encrypt these.
* Either with the user's private key or key derived from a username/password
*/
function addKey(user, key, nonce) {
let keyStore = JSON.parse(localStorage.getItem('keystore') || '{}')
keyStore[user] = {key, nonce}
localStorage.setItem('keystore', JSON.stringify(keyStore))
}

function getKey(user) {
let keyStore = JSON.parse(localStorage.getItem('keystore') || '{}')
return keyStore[user]
}

export default new Backend()
21 changes: 0 additions & 21 deletions frontend/src/components/AcceptButton.vue

This file was deleted.

26 changes: 0 additions & 26 deletions frontend/src/components/FollowButton.vue

This file was deleted.

24 changes: 24 additions & 0 deletions frontend/src/components/FollowRequestListItem.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<tr>
<td>{{ request.follower }}</td>
<td>
<button @click="accept">Accept</button>
</td>
</tr>
</template>

<script>
import Backend from '@/Backend'
export default {
name: 'FollowRequestListItem',
props: ['request'],
methods: {
accept: async function() {
await Backend.accept(this.request.follower)
this.$emit('update')
}
},
}
</script>
30 changes: 30 additions & 0 deletions frontend/src/components/FollowingListItem.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<template>
<tr>
<td>{{ user.follower }}</td>
<td>
<button v-if="shouldDisplayButton" @click="requestAccess">Request Access</button>
</td>
</tr>
</template>

<script>
import Backend from '@/Backend'
export default {
name: 'FollowRequestListItem',
props: ['user'],
methods: {
requestAccess: async function() {
await Backend.requestAccess(this.user.follower)
this.$emit('update')
}
},
computed: {
shouldDisplayButton: function() {
console.log("shouldDisplayButton called this.user.access_granted: ", this.user.access_granted)
return this.user.access_granted
}
}
}
</script>
26 changes: 16 additions & 10 deletions frontend/src/components/UserListItem.vue
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
<template>
<tr>
<td v-if="user.user">{{ user.user }}</td>
<td v-else-if="user.follower">{{ user.follower }}</td>
<td>{{ user.user }}</td>
<td>
<AcceptButton v-if="is_request" v-bind:user="user" />
<FollowButton v-else v-bind:user="user" />
<span v-if="isMyself">That's you!</span>
<span v-else-if="user.already_follows">Already following</span>
<span v-else-if="user.already_requested">Already requested</span>
<button v-else @click="follow">Follow</button>
</td>
</tr>
</template>

<script>
import Backend from '@/Backend'
import FollowButton from '@/components/FollowButton.vue'
import AcceptButton from '@/components/AcceptButton.vue'
export default {
name: 'UserListItem',
components: {
FollowButton,
AcceptButton,
props: ['user'],
methods: {
follow: async function() {
await Backend.followRequest(this.user.user)
this.$emit('update')
}
},
props: ['user', 'is_request'],
computed: {
isMyself: function() {
return Backend.account.name == this.user.user
}
}
}
</script>
26 changes: 19 additions & 7 deletions frontend/src/views/Users.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
<div id="user_list_container">
<h3>All Users</h3>
<table>
<UserListItem v-for="user in users" :user="user" v-on:update="update" />
<UserListItem v-for="user in users" :user="user" v-on:update="update" :key="user.user" />
</table>
</div>
<div id="follow_requests_container">
<div v-if="followRequests && followRequests.length" id="follow_requests_container">
<h3>Follow Requests</h3>
<table>
<UserListItem v-for="user in followRequests" :user="user" :is_request="true" v-on:update="update" />
<FollowRequestListItem v-for="request in followRequests" :request="request" v-on:update="update" :key="request.follower"/>
</table>
</div>
<div v-if="following && following.length" id="following_list_container">
<h3>Users you are following</h3>
<table>
<FollowingListItem v-for="user in following" :user="user" v-on:update="update" :key="user.followee" />
</table>
</div>
</div>
Expand All @@ -18,28 +24,34 @@
<script>
import Backend from '@/Backend'
import UserListItem from '@/components/UserListItem.vue'
import FollowRequestListItem from '@/components/FollowRequestListItem.vue'
import FollowingListItem from '@/components/FollowingListItem.vue'
async function reload(x) {
await Backend.scatterConnect()
x.users = await Backend.users()
x.following = await Backend.following(Backend.account.name)
x.followRequests = await Backend.followRequests(Backend.account.name)
x.following = await Backend.following()
x.followRequests = await Backend.followRequests()
console.log("this.users: ", x.users)
console.log("x.followRequests: ", x.followRequests)
}
export default {
name: 'Users',
components: {
UserListItem,
FollowRequestListItem,
FollowingListItem,
},
data() {
return {
users: null,
followRequests: null,
following: null,
}
},
async mounted() {
reload(this)
async beforeMount() {
await reload(this)
},
methods: {
update: function() {
Expand Down

0 comments on commit 555c2a7

Please sign in to comment.