Skip to content
Snippets Groups Projects
Commit 2a46c45b authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

wip merge membership traits

parent f0dbc0b2
Branches
No related tags found
No related merge requests found
...@@ -97,6 +97,12 @@ pub mod pallet { ...@@ -97,6 +97,12 @@ pub mod pallet {
#[pallet::error] #[pallet::error]
pub enum Error<T, I = ()> { pub enum Error<T, I = ()> {
/// Identity not allowed to claim membership
IdtyNotAllowedToClaimMembership,
/// Identity not allowed to request membership
IdtyNotAllowedToRequestMembership,
/// Identity not allowed to renew membership
IdtyNotAllowedToRenewMembership,
/// Identity creation period not respected /// Identity creation period not respected
IdtyCreationPeriodNotRespected, IdtyCreationPeriodNotRespected,
/// Not enough received certifications to create identity /// Not enough received certifications to create identity
...@@ -215,36 +221,40 @@ impl<T: Config<I>, I: 'static> pallet_certification::traits::CheckCertAllowed<Id ...@@ -215,36 +221,40 @@ impl<T: Config<I>, I: 'static> pallet_certification::traits::CheckCertAllowed<Id
} }
} }
impl<T: Config<I>, I: 'static> sp_membership::traits::IsIdtyAllowedToClaimMembership<IdtyIndex> impl<T: Config<I>, I: 'static> sp_membership::traits::CheckIdtyAllowedMembership<IdtyIndex>
for Pallet<T, I> for Pallet<T, I>
{ {
fn is_idty_allowed_to_claim_membership(idty_index: &IdtyIndex) -> bool { fn check_idty_allowed_to_claim_membership(idty_index: &IdtyIndex) -> Result<(), DispatchError> {
let idty_cert_meta = pallet_certification::Pallet::<T, I>::idty_cert_meta(idty_index); let idty_cert_meta = pallet_certification::Pallet::<T, I>::idty_cert_meta(idty_index);
idty_cert_meta.received_count >= T::MinCertForMembership::get() as u32 ensure!(
} idty_cert_meta.received_count >= T::MinCertForMembership::get() as u32,
Error::<T, I>::IdtyNotAllowedToClaimMembership
);
Ok(())
} }
impl<T: Config<I>, I: 'static> sp_membership::traits::IsIdtyAllowedToRenewMembership<IdtyIndex> fn check_idty_allowed_to_renew_membership(idty_index: &IdtyIndex) -> Result<(), DispatchError> {
for Pallet<T, I>
{
fn is_idty_allowed_to_renew_membership(idty_index: &IdtyIndex) -> bool {
if let Some(idty_value) = pallet_identity::Pallet::<T>::identity(idty_index) { if let Some(idty_value) = pallet_identity::Pallet::<T>::identity(idty_index) {
idty_value.status == IdtyStatus::Validated ensure!(
} else { idty_value.status == IdtyStatus::Validated,
false Error::<T, I>::IdtyNotAllowedToRenewMembership
} );
} }
// FIXME what if identity not found?
Ok(())
} }
impl<T: Config<I>, I: 'static> sp_membership::traits::IsIdtyAllowedToRequestMembership<IdtyIndex> fn check_idty_allowed_to_request_membership(
for Pallet<T, I> idty_index: &IdtyIndex,
{ ) -> Result<(), DispatchError> {
fn is_idty_allowed_to_request_membership(idty_index: &IdtyIndex) -> bool {
if let Some(idty_value) = pallet_identity::Pallet::<T>::identity(idty_index) { if let Some(idty_value) = pallet_identity::Pallet::<T>::identity(idty_index) {
T::IsSubWot::get() && idty_value.status == IdtyStatus::Validated ensure!(
} else { T::IsSubWot::get() && idty_value.status == IdtyStatus::Validated,
false Error::<T, I>::IdtyNotAllowedToRequestMembership
);
} }
// FIXME what if identity not found?
Ok(())
} }
} }
......
...@@ -142,9 +142,7 @@ parameter_types! { ...@@ -142,9 +142,7 @@ parameter_types! {
} }
impl pallet_membership::Config<Instance1> for Test { impl pallet_membership::Config<Instance1> for Test {
type IsIdtyAllowedToClaimMembership = DuniterWot; type CheckIdtyAllowedMembership = DuniterWot;
type IsIdtyAllowedToRenewMembership = DuniterWot;
type IsIdtyAllowedToRequestMembership = DuniterWot;
type Event = Event; type Event = Event;
type IdtyId = IdtyIndex; type IdtyId = IdtyIndex;
type IdtyIdOf = IdentityIndexOf<Self>; type IdtyIdOf = IdentityIndexOf<Self>;
...@@ -196,9 +194,7 @@ parameter_types! { ...@@ -196,9 +194,7 @@ parameter_types! {
} }
impl pallet_membership::Config<Instance2> for Test { impl pallet_membership::Config<Instance2> for Test {
type IsIdtyAllowedToClaimMembership = SmithsSubWot; type CheckIdtyAllowedMembership = SmithsSubWot;
type IsIdtyAllowedToRenewMembership = SmithsSubWot;
type IsIdtyAllowedToRequestMembership = SmithsSubWot;
type Event = Event; type Event = Event;
type IdtyId = IdtyIndex; type IdtyId = IdtyIndex;
type IdtyIdOf = IdentityIndexOf<Self>; type IdtyIdOf = IdentityIndexOf<Self>;
......
...@@ -59,12 +59,8 @@ pub mod pallet { ...@@ -59,12 +59,8 @@ pub mod pallet {
#[pallet::config] #[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config { pub trait Config<I: 'static = ()>: frame_system::Config {
/// Ask the runtime if the identity can claim the membership /// Ask the runtime if the identity can perform membership operations
type IsIdtyAllowedToClaimMembership: IsIdtyAllowedToClaimMembership<Self::IdtyId>; type CheckIdtyAllowedMembership: CheckIdtyAllowedMembership<Self::IdtyId>;
/// Ask the runtime if the identity can renew his membership
type IsIdtyAllowedToRenewMembership: IsIdtyAllowedToRenewMembership<Self::IdtyId>;
/// Ask the runtime if the identity can request the membership
type IsIdtyAllowedToRequestMembership: IsIdtyAllowedToRequestMembership<Self::IdtyId>;
/// Because this pallet emits events, it depends on the runtime's definition of an event. /// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::Event>; type Event: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::Event>;
/// Something that identifies an identity /// Something that identifies an identity
...@@ -160,12 +156,6 @@ pub mod pallet { ...@@ -160,12 +156,6 @@ pub mod pallet {
#[pallet::error] #[pallet::error]
pub enum Error<T, I = ()> { pub enum Error<T, I = ()> {
/// Identity not allowed to claim membership
IdtyNotAllowedToClaimMembership,
/// Identity not allowed to request membership
IdtyNotAllowedToRequestMembership,
/// Identity not allowed to renew membership
IdtyNotAllowedToRenewMembership,
/// Invalid meta data /// Invalid meta data
InvalidMetaData, InvalidMetaData,
/// Identity id not found /// Identity id not found
...@@ -221,10 +211,7 @@ pub mod pallet { ...@@ -221,10 +211,7 @@ pub mod pallet {
if !metadata.validate(&who) { if !metadata.validate(&who) {
return Err(Error::<T, I>::InvalidMetaData.into()); return Err(Error::<T, I>::InvalidMetaData.into());
} }
if !T::IsIdtyAllowedToRequestMembership::is_idty_allowed_to_request_membership(&idty_id) T::CheckIdtyAllowedMembership::check_idty_allowed_to_request_membership(&idty_id)?;
{
return Err(Error::<T, I>::IdtyNotAllowedToRequestMembership.into());
}
Self::do_request_membership(idty_id, metadata) Self::do_request_membership(idty_id, metadata)
} }
...@@ -242,10 +229,7 @@ pub mod pallet { ...@@ -242,10 +229,7 @@ pub mod pallet {
Error::<T, I>::MembershipAlreadyAcquired Error::<T, I>::MembershipAlreadyAcquired
); );
ensure!( T::CheckIdtyAllowedMembership::check_idty_allowed_to_claim_membership(&idty_id)?;
T::IsIdtyAllowedToClaimMembership::is_idty_allowed_to_claim_membership(&idty_id),
Error::<T, I>::IdtyNotAllowedToClaimMembership
);
let metadata = PendingMembership::<T, I>::take(&idty_id) let metadata = PendingMembership::<T, I>::take(&idty_id)
.ok_or(Error::<T, I>::MembershipRequestNotFound)?; .ok_or(Error::<T, I>::MembershipRequestNotFound)?;
...@@ -271,10 +255,7 @@ pub mod pallet { ...@@ -271,10 +255,7 @@ pub mod pallet {
Error::<T, I>::MembershipNotFound Error::<T, I>::MembershipNotFound
); );
ensure!( T::CheckIdtyAllowedMembership::check_idty_allowed_to_renew_membership(&idty_id)?;
T::IsIdtyAllowedToRenewMembership::is_idty_allowed_to_renew_membership(&idty_id),
Error::<T, I>::IdtyNotAllowedToRenewMembership,
);
let _ = Self::do_renew_membership(idty_id); let _ = Self::do_renew_membership(idty_id);
......
...@@ -83,9 +83,7 @@ parameter_types! { ...@@ -83,9 +83,7 @@ parameter_types! {
} }
impl pallet_membership::Config for Test { impl pallet_membership::Config for Test {
type IsIdtyAllowedToClaimMembership = (); type CheckIdtyAllowedMembership = ();
type IsIdtyAllowedToRenewMembership = ();
type IsIdtyAllowedToRequestMembership = ();
type Event = Event; type Event = Event;
type IdtyId = IdtyId; type IdtyId = IdtyId;
type IdtyIdOf = ConvertInto; type IdtyIdOf = ConvertInto;
......
...@@ -14,35 +14,23 @@ ...@@ -14,35 +14,23 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>. // along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.
use frame_support::pallet_prelude::Weight; use frame_support::pallet_prelude::*;
pub trait IsIdtyAllowedToClaimMembership<IdtyId> { pub trait CheckIdtyAllowedMembership<IdtyId> {
fn is_idty_allowed_to_claim_membership(idty_id: &IdtyId) -> bool; fn check_idty_allowed_to_claim_membership(idty_id: &IdtyId) -> Result<(), DispatchError>;
fn check_idty_allowed_to_renew_membership(idty_id: &IdtyId) -> Result<(), DispatchError>;
fn check_idty_allowed_to_request_membership(idty_id: &IdtyId) -> Result<(), DispatchError>;
} }
impl<IdtyId> IsIdtyAllowedToClaimMembership<IdtyId> for () { impl<IdtyId> CheckIdtyAllowedMembership<IdtyId> for () {
fn is_idty_allowed_to_claim_membership(_: &IdtyId) -> bool { fn check_idty_allowed_to_claim_membership(_: &IdtyId) -> Result<(), DispatchError> {
true Ok(())
}
}
pub trait IsIdtyAllowedToRenewMembership<IdtyId> {
fn is_idty_allowed_to_renew_membership(idty_id: &IdtyId) -> bool;
} }
fn check_idty_allowed_to_renew_membership(_: &IdtyId) -> Result<(), DispatchError> {
impl<IdtyId> IsIdtyAllowedToRenewMembership<IdtyId> for () { Ok(())
fn is_idty_allowed_to_renew_membership(_: &IdtyId) -> bool {
true
} }
} fn check_idty_allowed_to_request_membership(_: &IdtyId) -> Result<(), DispatchError> {
Ok(())
pub trait IsIdtyAllowedToRequestMembership<IdtyId> {
fn is_idty_allowed_to_request_membership(idty_id: &IdtyId) -> bool;
}
impl<IdtyId> IsIdtyAllowedToRequestMembership<IdtyId> for () {
fn is_idty_allowed_to_request_membership(_: &IdtyId) -> bool {
true
} }
} }
......
...@@ -444,9 +444,7 @@ macro_rules! pallets_config { ...@@ -444,9 +444,7 @@ macro_rules! pallets_config {
} }
impl pallet_membership::Config<frame_support::instances::Instance1> for Runtime { impl pallet_membership::Config<frame_support::instances::Instance1> for Runtime {
type IsIdtyAllowedToClaimMembership = Wot; type CheckIdtyAllowedMembership = Wot;
type IsIdtyAllowedToRenewMembership = Wot;
type IsIdtyAllowedToRequestMembership = Wot;
type Event = Event; type Event = Event;
type IdtyId = IdtyIndex; type IdtyId = IdtyIndex;
type IdtyIdOf = common_runtime::providers::IdentityIndexOf<Self>; type IdtyIdOf = common_runtime::providers::IdentityIndexOf<Self>;
...@@ -480,9 +478,7 @@ macro_rules! pallets_config { ...@@ -480,9 +478,7 @@ macro_rules! pallets_config {
} }
impl pallet_membership::Config<Instance2> for Runtime { impl pallet_membership::Config<Instance2> for Runtime {
type IsIdtyAllowedToClaimMembership = SmithsSubWot; type CheckIdtyAllowedMembership = SmithsSubWot;
type IsIdtyAllowedToRenewMembership = SmithsSubWot;
type IsIdtyAllowedToRequestMembership = SmithsSubWot;
type Event = Event; type Event = Event;
type IdtyId = IdtyIndex; type IdtyId = IdtyIndex;
type IdtyIdOf = common_runtime::providers::IdentityIndexOf<Self>; type IdtyIdOf = common_runtime::providers::IdentityIndexOf<Self>;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment