Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit a0a39bc

Browse files
authored
Merge pull request #1 from aphelionz/feat/permissions
feat: basic permissions
2 parents 0dd5363 + 83c6ea7 commit a0a39bc

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed

src/components/CreateDialog.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react'
2-
import {
3-
Dialog,
4-
Select,
5-
TextInput
2+
import {
3+
Dialog,
4+
FormField,
5+
Select,
6+
TextInput
67
} from 'evergreen-ui'
78

89
import { useStateValue, actions } from '../state'
@@ -11,12 +12,13 @@ function CreateDialog ({ onCreate }) {
1112
const [appState, dispatch] = useStateValue()
1213
const [name, setName] = React.useState('')
1314
const [type, setType] = React.useState('eventlog')
15+
const [permissions, setPermissions] = React.useState('creator')
1416

1517
function handleSubmit (event) {
1618
if (event) event.preventDefault()
1719
if (name.length === 0) return
18-
console.log("Create:", name, type)
19-
onCreate({ name, type })
20+
console.log('Create:', name, type, permissions)
21+
onCreate({ name, type, permissions })
2022
dispatch({ type: actions.DB.CLOSE_CREATEDB_DIALOG })
2123
}
2224

@@ -28,6 +30,10 @@ function CreateDialog ({ onCreate }) {
2830
setType(event.target.value)
2931
}
3032

33+
function handlePermissionsChange (event) {
34+
setPermissions(event.target.value)
35+
}
36+
3137
return (
3238
<Dialog
3339
isShown={appState.createDBDialogOpen}
@@ -38,22 +44,29 @@ function CreateDialog ({ onCreate }) {
3844
onConfirm={close => handleSubmit(null, close)}
3945
>
4046
<form onSubmit={handleSubmit}>
47+
<FormField label='Database Name:'>
4148
<TextInput
4249
onChange={handleNameChange}
4350
name='name'
4451
placeholder='Database name'
4552
width='100%'
46-
></TextInput>
47-
<span>Type:</span>
48-
<div>
49-
<Select onChange={handleTypeChange}>
50-
<option value='eventlog' defaultValue>Immutable Log</option>
51-
<option value='feed'>A list of entries</option>
52-
<option value='keyvalue'>Key-Value Store</option>
53-
<option value='docstore'>Document Store</option>
54-
<option value='counter'>Counter (CRDT)</option>
55-
</Select>
56-
</div>
53+
/>
54+
</FormField>
55+
<FormField label='Type:'>
56+
<Select onChange={handleTypeChange}>
57+
<option value='eventlog' defaultValue>Immutable Log</option>
58+
<option value='feed'>A list of entries</option>
59+
<option value='keyvalue'>Key-Value Store</option>
60+
<option value='docstore'>Document Store</option>
61+
<option value='counter'>Counter (CRDT)</option>
62+
</Select>
63+
</FormField>
64+
<FormField label='Write Permissions'>
65+
<Select onChange={handlePermissionsChange}>
66+
<option value='creator'>Creator-only: Only you can write, public read</option>
67+
<option value='public'>Public: Anybody can write and write</option>
68+
</Select>
69+
</FormField>
5770
</form>
5871
</Dialog>
5972
)

src/database/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,21 @@ export const addDatabase = async (address) => {
5353
})
5454
}
5555

56-
export const createDatabase = async (name, type) => {
57-
const db = await orbitdb.create(name, type)
58-
return programs.add({
56+
export const createDatabase = async (name, type, permissions) => {
57+
let accessController
58+
59+
switch (permissions) {
60+
case 'public':
61+
accessController = { write: ['*'] }
62+
break
63+
default:
64+
accessController = { write: [orbitdb.identity.id] }
65+
break
66+
}
67+
68+
const db = await orbitdb.create(name, type, { accessController })
69+
70+
return programs.add({
5971
name,
6072
type,
6173
address: db.address.toString(),

src/views/Database.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,16 @@ function ProgramView () {
109109
: <Text>-</Text>
110110
}
111111
</Pane>
112+
<Pane flex='1'_>
113+
<Text>Permissions:</Text>
114+
{appState.db
115+
? <pre>{appState.db.access.write}</pre>
116+
: <Text>-</Text>
117+
}
118+
</Pane>
112119
<Pane flex='1' flexDirection='row'>
113120
<Text>Entries: </Text>
114-
{appState.db
121+
{appState.db
115122
? <Text>{appState.db._oplog.length}</Text>
116123
: <Text>-</Text>
117124
}

src/views/Databases.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function DatabasesView () {
3434

3535
const createDB = (args) => {
3636
console.log("Create database...", args)
37-
createDatabase(args.name, args.type).then((hash) => {
37+
createDatabase(args.name, args.type, args.permissions).then((hash) => {
3838
console.log("Created", hash)
3939
fetchDatabases().then((data) => {
4040
console.log("Loaded programs", data)

0 commit comments

Comments
 (0)