Search in sources :

Example 6 with UserRoles

use of com.jd.blockchain.ledger.UserRoles in project jdchain-core by blockchain-jd-com.

the class UserRoleDatasetEditor method getUserRoles.

/**
 * 查询角色授权;
 *
 * <br>
 * 如果不存在,则返回 null;
 *
 * @param address
 * @return
 */
@Override
public UserRoles getUserRoles(Bytes userAddress) {
    // 只返回最新版本;
    DataEntry<Bytes, byte[]> kv = dataset.getDataEntry(userAddress);
    if (kv == null) {
        return null;
    }
    RoleSet roleSet = BinaryProtocol.decode(kv.getValue());
    return new UserRoles(userAddress, kv.getVersion(), roleSet);
}
Also used : Bytes(utils.Bytes) RoleSet(com.jd.blockchain.ledger.RoleSet) UserRoles(com.jd.blockchain.ledger.UserRoles)

Example 7 with UserRoles

use of com.jd.blockchain.ledger.UserRoles in project jdchain-core by blockchain-jd-com.

the class UserRoleDatasetEditor method getUserRoles.

@Override
public UserRoles[] getUserRoles() {
    DataEntry<Bytes, byte[]>[] kvEntries = dataset.getDataEntries(0, (int) dataset.getDataCount());
    UserRoles[] pns = new UserRoles[kvEntries.length];
    RoleSet roleset;
    for (int i = 0; i < pns.length; i++) {
        roleset = BinaryProtocol.decode(kvEntries[i].getValue());
        pns[i] = new UserRoles(kvEntries[i].getKey(), kvEntries[i].getVersion(), roleset);
    }
    return pns;
}
Also used : DataEntry(utils.DataEntry) RoleSet(com.jd.blockchain.ledger.RoleSet) UserRoles(com.jd.blockchain.ledger.UserRoles)

Example 8 with UserRoles

use of com.jd.blockchain.ledger.UserRoles in project jdchain-core by blockchain-jd-com.

the class UserRoleDatasetTest method testAddUserRoles.

@Test
public void testAddUserRoles() {
    CryptoConfig cryptoConfig = new CryptoConfig();
    cryptoConfig.setAutoVerifyHash(true);
    cryptoConfig.setSupportedProviders(SUPPORTED_PROVIDERS);
    cryptoConfig.setHashAlgorithm(HASH_ALGORITHM);
    MemoryKVStorage testStorage = new MemoryKVStorage();
    String prefix = "user-roles/";
    UserRoleDatasetEditor userRolesDataset = new UserRoleDatasetEditor(cryptoConfig, prefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE);
    BlockchainKeypair bckp = BlockchainKeyGenerator.getInstance().generate();
    String[] authRoles = { "DEFAULT", "MANAGER" };
    userRolesDataset.addUserRoles(bckp.getAddress(), RolesPolicy.UNION, authRoles);
    userRolesDataset.commit();
    assertEquals(1, userRolesDataset.getUserCount());
    UserRoles userRoles = userRolesDataset.getUserRoles(bckp.getAddress());
    assertNotNull(userRoles);
    String[] roles = userRoles.getRoles();
    assertEquals(2, roles.length);
    assertArrayEquals(authRoles, roles);
    assertEquals(RolesPolicy.UNION, userRoles.getPolicy());
}
Also used : UserRoleDatasetEditor(com.jd.blockchain.ledger.core.UserRoleDatasetEditor) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) UserRoles(com.jd.blockchain.ledger.UserRoles) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) Test(org.junit.Test)

Aggregations

UserRoles (com.jd.blockchain.ledger.UserRoles)8 Bytes (utils.Bytes)4 AuthorizationException (com.jd.blockchain.ledger.AuthorizationException)2 RolePrivilegeSettings (com.jd.blockchain.ledger.RolePrivilegeSettings)2 RoleSet (com.jd.blockchain.ledger.RoleSet)2 UserAuthorizationSettings (com.jd.blockchain.ledger.UserAuthorizationSettings)2 UserRoleDatasetEditor (com.jd.blockchain.ledger.core.UserRoleDatasetEditor)2 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)1 RoleDoesNotExistException (com.jd.blockchain.ledger.RoleDoesNotExistException)1 RolePrivileges (com.jd.blockchain.ledger.RolePrivileges)1 RolesPolicy (com.jd.blockchain.ledger.RolesPolicy)1 SecurityPolicy (com.jd.blockchain.ledger.SecurityPolicy)1 UserRolesEntry (com.jd.blockchain.ledger.UserAuthorizeOperation.UserRolesEntry)1 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)1 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)1 Test (org.junit.Test)1 DataEntry (utils.DataEntry)1