COOPENOMICS  v1
Кооперативная Экономика
table_branch_branches.hpp
См. документацию.
1#pragma once
2
3#include <algorithm>
4#include <eosio/binary_extension.hpp>
5#include <eosio/eosio.hpp>
6#include <vector>
7
8#include "../consts.hpp"
9
15struct [[eosio::table, eosio::contract(BRANCH)]] coobranch {
16 eosio::name braname;
17 eosio::name trustee;
18 std::vector<eosio::name> trusted;
19
20 // Поля приватности добавлены как binary_extension: у действующих кооперативов
21 // в таблице уже могут быть строки, и расширение в конце struct сохраняет
22 // обратную совместимость десериализации (старые записи читаются как публичные с пустым списком).
23 eosio::binary_extension<bool> is_private; // приватный участок: выбрать его могут только из белого списка
24 eosio::binary_extension<std::vector<eosio::name>> whitelist; // белый список аккаунтов, допущенных к выбору приватного участка
25
26 uint64_t primary_key() const { return braname.value; }
27 uint64_t by_trustee() const { return trustee.value; }
28
29 void add_account_to_trusted(const eosio::name &account) { trusted.push_back(account); }
30
31 void remove_account_from_trusted(const eosio::name &account) {
32 auto itr = std::remove(trusted.begin(), trusted.end(), account);
33 eosio::check(itr != trusted.end(), "Account not found in trusted list");
34 trusted.erase(itr, trusted.end());
35 }
36
37 bool is_account_in_trusted(const eosio::name &account) const {
38 return std::find(trusted.begin(), trusted.end(), account) != trusted.end();
39 }
40
41 bool is_user_authorized(const eosio::name &username) const {
42 if (trustee == username) {
43 return true;
44 }
45 return is_account_in_trusted(username);
46 }
47
48 // признак приватности с учётом отсутствующего расширения у старых записей
49 // (const-перегрузка value_or() без аргумента возвращает bool() == false, если значения нет)
50 bool is_branch_private() const { return is_private.value_or(); }
51
52 bool is_account_in_whitelist(const eosio::name &account) const {
53 if (!whitelist.has_value()) {
54 return false;
55 }
56 const auto &wl = whitelist.value();
57 return std::find(wl.begin(), wl.end(), account) != wl.end();
58 }
59
60 void set_private(bool value) { is_private.emplace(value); }
61
62 void add_account_to_whitelist(const eosio::name &account) {
63 if (!whitelist.has_value()) {
64 whitelist.emplace();
65 }
66 auto &wl = whitelist.value();
67 eosio::check(std::find(wl.begin(), wl.end(), account) == wl.end(),
68 "Аккаунт уже добавлен в белый список кооперативного участка");
69 wl.push_back(account);
70 }
71
72 void remove_account_from_whitelist(const eosio::name &account) {
73 eosio::check(whitelist.has_value(), "Белый список кооперативного участка пуст");
74 auto &wl = whitelist.value();
75 auto it = std::find(wl.begin(), wl.end(), account);
76 eosio::check(it != wl.end(), "Аккаунт не найден в белом списке кооперативного участка");
77 wl.erase(it);
78 }
79};
80
81typedef eosio::multi_index<
82 "branches"_n, coobranch,
83 eosio::indexed_by<"bytrustee"_n, eosio::const_mem_fun<coobranch, uint64_t, &coobranch::by_trustee>>>
85
86coobranch get_branch_or_fail(eosio::name coopname, eosio::name braname) {
87 branch_index branches(_branch, coopname.value);
88 auto branch = branches.find(braname.value);
89
90 eosio::check(branch != branches.end(), "Кооперативный Участок не найден");
91
92 return *branch;
93}
Константы контракта кооперативных участков
Definition: branch.hpp:55
static constexpr eosio::name _branch
Definition: consts.hpp:176
contract
Definition: eosio.msig_tests.cpp:977
Definition: eosio.msig.hpp:34
Definition: table_registrator_accounts.hpp:14
Definition: table_branch_branches.hpp:15
void set_private(bool value)
Definition: table_branch_branches.hpp:60
eosio::binary_extension< bool > is_private
Definition: table_branch_branches.hpp:23
uint64_t by_trustee() const
Definition: table_branch_branches.hpp:27
void remove_account_from_whitelist(const eosio::name &account)
Definition: table_branch_branches.hpp:72
void add_account_to_trusted(const eosio::name &account)
Definition: table_branch_branches.hpp:29
bool is_branch_private() const
Definition: table_branch_branches.hpp:50
void add_account_to_whitelist(const eosio::name &account)
Definition: table_branch_branches.hpp:62
uint64_t primary_key() const
Definition: table_branch_branches.hpp:26
void remove_account_from_trusted(const eosio::name &account)
Definition: table_branch_branches.hpp:31
bool is_user_authorized(const eosio::name &username) const
Definition: table_branch_branches.hpp:41
bool is_account_in_trusted(const eosio::name &account) const
Definition: table_branch_branches.hpp:37
eosio::binary_extension< std::vector< eosio::name > > whitelist
Definition: table_branch_branches.hpp:24
eosio::name trustee
Definition: table_branch_branches.hpp:17
std::vector< eosio::name > trusted
Definition: table_branch_branches.hpp:18
bool is_account_in_whitelist(const eosio::name &account) const
Definition: table_branch_branches.hpp:52
eosio::name braname
Definition: table_branch_branches.hpp:16
coobranch get_branch_or_fail(eosio::name coopname, eosio::name braname)
Definition: table_branch_branches.hpp:86
eosio::multi_index< "branches"_n, coobranch, eosio::indexed_by<"bytrustee"_n, eosio::const_mem_fun< coobranch, uint64_t, &coobranch::by_trustee > > > branch_index
Definition: table_branch_branches.hpp:84