mirror of
https://github.com/etesync/android
synced 2025-01-11 08:10:58 +00:00
Journalmanager: Add api for the members endpoint.
This API controls the members of a journal, that is, access control.
This commit is contained in:
parent
11e37dbd1e
commit
efe832ddb4
@ -26,6 +26,8 @@ import static com.etesync.syncadapter.journalmanager.Crypto.toHex;
|
|||||||
public class JournalManager extends BaseManager {
|
public class JournalManager extends BaseManager {
|
||||||
final static private Type journalType = new TypeToken<List<Journal>>() {
|
final static private Type journalType = new TypeToken<List<Journal>>() {
|
||||||
}.getType();
|
}.getType();
|
||||||
|
final static private Type memberType = new TypeToken<List<Member>>() {
|
||||||
|
}.getType();
|
||||||
|
|
||||||
|
|
||||||
public JournalManager(OkHttpClient httpClient, HttpUrl remote) {
|
public JournalManager(OkHttpClient httpClient, HttpUrl remote) {
|
||||||
@ -90,6 +92,43 @@ public class JournalManager extends BaseManager {
|
|||||||
newCall(request);
|
newCall(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private HttpUrl getMemberRemote(Journal journal) {
|
||||||
|
return this.remote.resolve(journal.getUid() + "/members/");
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Member> listMembers(Journal journal) throws Exceptions.HttpException, Exceptions.IntegrityException, Exceptions.GenericCryptoException {
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.get()
|
||||||
|
.url(getMemberRemote(journal))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Response response = newCall(request);
|
||||||
|
ResponseBody body = response.body();
|
||||||
|
return GsonHelper.gson.fromJson(body.charStream(), memberType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteMember(Journal journal, Member member) throws Exceptions.HttpException {
|
||||||
|
RequestBody body = RequestBody.create(JSON, member.toJson());
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.delete(body)
|
||||||
|
.url(getMemberRemote(journal))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
newCall(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addMember(Journal journal, Member member) throws Exceptions.HttpException {
|
||||||
|
RequestBody body = RequestBody.create(JSON, member.toJson());
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.post(body)
|
||||||
|
.url(getMemberRemote(journal))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
newCall(request);
|
||||||
|
}
|
||||||
|
|
||||||
public static class Journal extends Base {
|
public static class Journal extends Base {
|
||||||
@Getter
|
@Getter
|
||||||
private int version = -1;
|
private int version = -1;
|
||||||
@ -140,4 +179,24 @@ public class JournalManager extends BaseManager {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Member {
|
||||||
|
@Getter
|
||||||
|
private String user;
|
||||||
|
@Getter
|
||||||
|
private byte[] key;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
private Member() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Member(String user, byte[] encryptedKey) {
|
||||||
|
this.user = user;
|
||||||
|
this.key = encryptedKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
String toJson() {
|
||||||
|
return GsonHelper.gson.toJson(this, getClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,6 @@ import okio.BufferedSink;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
@ -232,4 +231,29 @@ public class ServiceTest {
|
|||||||
userInfo = manager.get(cryptoManager, Helpers.USER);
|
userInfo = manager.get(cryptoManager, Helpers.USER);
|
||||||
assertNull(userInfo);
|
assertNull(userInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJournalMember() throws IOException, Exceptions.HttpException, Exceptions.GenericCryptoException, Exceptions.IntegrityException {
|
||||||
|
JournalManager journalManager = new JournalManager(httpClient, remote);
|
||||||
|
CollectionInfo info = CollectionInfo.defaultForServiceType(CollectionInfo.Type.ADDRESS_BOOK);
|
||||||
|
info.uid = JournalManager.Journal.genUid();
|
||||||
|
info.displayName = "Test";
|
||||||
|
Crypto.CryptoManager crypto = new Crypto.CryptoManager(info.version, Helpers.keyBase64, info.uid);
|
||||||
|
JournalManager.Journal journal = new JournalManager.Journal(crypto, info.toJson(), info.uid);
|
||||||
|
journalManager.putJournal(journal);
|
||||||
|
|
||||||
|
assertEquals(journalManager.listMembers(journal).size(), 0);
|
||||||
|
|
||||||
|
// Test inviting ourselves
|
||||||
|
JournalManager.Member member = new JournalManager.Member(Helpers.USER, "test".getBytes(Charsets.UTF_8));
|
||||||
|
journalManager.addMember(journal, member);
|
||||||
|
|
||||||
|
assertEquals(journalManager.listMembers(journal).size(), 1);
|
||||||
|
|
||||||
|
// Uninviting ourselves
|
||||||
|
journalManager.deleteMember(journal, member);
|
||||||
|
|
||||||
|
assertEquals(journalManager.listMembers(journal).size(), 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user