Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.21;
- contract asus{
- //表示授權多久&授權區間
- struct Authorized_times {
- uint expiration_time;
- uint access_period_start_time;
- uint access_period_end_time;
- }
- //映射關係,從address->apps'id->data category->時間
- mapping (address => mapping(string => mapping(string => Authorized_times))) authorizations;
- //映射關係,從user's_id->service_name->blockchain_address table
- mapping (string => mapping(string => address)) id_mapping_table;
- //創建使用者給APP授權的事件
- event create_auth(
- address blockchain_address,
- string app_id,
- string data_category,
- uint expiration_time,
- uint access_period_start_time,
- uint access_period_end_time
- );
- //創建使用者給APP的授權
- function create_authorization(address blockchain_address, string app_id, string data_category, uint time_num, uint begin_date, uint end_date) public {
- require(
- msg.sender == blockchain_address,
- "You can only authorize your own account. "
- );
- authorizations[blockchain_address][app_id][data_category].expiration_time = now + time_num * 1 days;
- authorizations[blockchain_address][app_id][data_category].access_period_start_time = begin_date;
- authorizations[blockchain_address][app_id][data_category].access_period_end_time = end_date;
- emit create_auth(blockchain_address, app_id, data_category,
- authorizations[blockchain_address][app_id][data_category].expiration_time,
- authorizations[blockchain_address][app_id][data_category].access_period_start_time,
- authorizations[blockchain_address][app_id][data_category].access_period_end_time);
- }
- //使用者修改授權效期的事件
- event modify_auth_time_expiration(
- address blockchain_address,
- string app_id,
- string data_category,
- uint expiration_time
- );
- //修改授權的效期時間
- function modify_authorization_time_expiration(address blockchain_address, string app_id, string data_category, uint time_num) public {
- require(
- msg.sender == blockchain_address,
- "You can only modify authorization on your own account. "
- );
- authorizations[blockchain_address][app_id][data_category].expiration_time = now + time_num * 1 days;
- emit modify_auth_time_expiration(blockchain_address, app_id, data_category,
- authorizations[blockchain_address][app_id][data_category].expiration_time);
- }
- //使用者修改授權起止時間的事件
- event modify_auth_time_period(
- address blockchain_address,
- string app_id,
- string data_category,
- uint access_period_start_time,
- uint access_period_end_time
- );
- //修改授權的起止時間
- function modify_authorization_time_period(address blockchain_address, string app_id, string data_category, uint begin_date, uint end_date) public {
- require(
- msg.sender == blockchain_address,
- "You can only modify authorization on your own account. "
- );
- authorizations[blockchain_address][app_id][data_category].access_period_start_time = begin_date;
- authorizations[blockchain_address][app_id][data_category].access_period_end_time = end_date;
- emit modify_auth_time_period(blockchain_address, app_id, data_category,
- authorizations[blockchain_address][app_id][data_category].access_period_start_time,
- authorizations[blockchain_address][app_id][data_category].access_period_end_time);
- }
- //查看授權的效期時間
- function get_time_expiration(address blockchain_address, string app_id, string data_category) public returns(uint exp_time) {
- require(
- msg.sender == blockchain_address,
- "You can only get your own account's message. "
- );
- return (authorizations[blockchain_address][app_id][data_category].expiration_time);
- }
- //查看授權的起止時間
- function get_time_period(address blockchain_address, string app_id, string data_category) public returns(uint d_begin, uint d_end) {
- require(
- msg.sender == blockchain_address,
- "You can only get your own account's message. "
- );
- return (authorizations[blockchain_address][app_id][data_category].access_period_start_time, authorizations[blockchain_address][app_id][data_category].access_period_end_time);
- }
- //判斷對APP是否有授權
- function verify_user_authorization(address blockchain_address, string app_id, string data_category, uint date_begin, uint date_end) public returns(bool){
- bool result = false;
- if(now <= authorizations[blockchain_address][app_id][data_category].expiration_time)
- {
- if(date_begin >= authorizations[blockchain_address][app_id][data_category].access_period_start_time && date_end <= authorizations[blockchain_address][app_id][data_category].access_period_end_time
- && date_begin <= date_end)
- {
- result = true;
- }
- }
- return result;
- }
- //新增address的事件
- event append_address_service (
- string user_id,
- string service_name,
- address blockchain_address
- );
- //在user_id新增一組service_name, blockchain address
- function append_ID_address_service_mapping_table(string user_id, string service_name, address blockchain_address)public{
- id_mapping_table[user_id][service_name] = blockchain_address;
- emit append_address_service(user_id, service_name, id_mapping_table[user_id][service_name]);
- }
- //刪除一組service_name, blockchain address的事件
- event delete_address_service (
- string user_id,
- string service_name,
- address before_delete_blockchain_address,
- address after_delete_blockchain_address
- );
- //在一個user id刪除一組service_name, blockchain address
- function delete_ID_address_service(string user_id, string service_name)public{
- address temp_blockchain_address;
- temp_blockchain_address = id_mapping_table[user_id][service_name];
- delete id_mapping_table[user_id][service_name];
- //很奇怪,這裡記錄日誌會和delete衝突,不能同時存在
- //emit delete_address_service(user_id, service_name, temp_blockchain_address, id_mapping_table[user_id][service_name]);
- }
- //得到 ID and blockchain address mapping table
- function get_ID_address_service_mapping_table(string user_id, string service_name)public returns(address){
- return id_mapping_table[user_id][service_name];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment