我想要构建一个简单的智能契约,它能够创建资产并将具有不同角色的用户添加到其中。每个用户应该只能在每个资产分配的时间内拥有一个角色。
稳固的代码:
contract AccessManagement {
struct Authorization {
string role;
bool active;
}
struct Asset {
address owner;
address[] authorizationList;
mapping(address => Authorization) authorizationStructs;
bool initialized;
}
mapping(string => Asset) assetStructs;
string[] assetList;
function newAsset(string assetKey) public returns(bool success) {
// Check for duplicates
assetStructs[assetKey].owner = msg.sender;
assetStructs[assetKey].initialized = true;
assetList.push(assetKey);
return true;
}
function addAuthorization(string assetKey, address authorizationKey, string authorizationRole) public returns(bool success) {
// ??? - Require Role "Admin"
// ??? - Push only if "authorizationKey" is unique. Otherwise change the values.
assetStructs[assetKey].authorizationList.push(authorizationKey);
assetStructs[assetKey].authorizationStructs[authorizationKey].role = authorizationRole;
assetStructs[assetKey].authorizationStructs[authorizationKey].active = true;
return true;
}
function getAssetAuthorization(string assetKey, address authorizationKey) public constant returns(string authorizationRole) {
return(assetStructs[assetKey].authorizationStructs[authorizationKey].role);
}
}我对此提出的问题:
发布于 2018-09-10 07:49:30
我没看到你在任何地方都在使用授权名单。你可以把它移走。为了确保始终只有一个授权密钥,您可以使用现有的映射。现有条目将被覆盖。当您想知道键是否存在时,请检查active是否为true,因为对于所有不存在的键,默认情况下都是false。您需要在某个时候迭代所有的地址吗?
要检查角色是否等于Admin,我将为角色创建一个枚举并与之进行比较。就像这样:
// Enum
enum Roles { User, Admin };
// Struct
struct Authorization {
Roles role;
bool active;
}
// Checking
if (assetStructs[assetKey].authorizationStructs[authorizationKey].role == Roles.Admin) {
// something here
}https://stackoverflow.com/questions/52137076
复制相似问题