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