Skip to content

Commit dbbc91a

Browse files
committed
adds RBAC validations on offer data flow
1 parent 1df09a7 commit dbbc91a

File tree

6 files changed

+19
-32
lines changed

6 files changed

+19
-32
lines changed

pallets/gated-marketplace/src/benchmarking.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

pallets/gated-marketplace/src/functions.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<T: Config> Pallet<T> {
183183
//This function is only called by the owner of the marketplace
184184
//ensure the marketplace exists
185185
ensure!(<Marketplaces<T>>::contains_key(marketplace_id), Error::<T>::MarketplaceNotFound);
186-
186+
Self::is_authorized(authority.clone(), &marketplace_id,Permission::EnlistSellOffer)?;
187187
//ensure the collection exists
188188
if let Some(a) = pallet_uniques::Pallet::<T>::owner(collection_id, item_id) {
189189
ensure!(a == authority, Error::<T>::NotOwner);
@@ -244,13 +244,14 @@ impl<T: Config> Pallet<T> {
244244
}
245245

246246
pub fn do_enlist_buy_offer(authority: T::AccountId, marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId, price: BalanceOf<T>,) -> DispatchResult {
247-
//TODO: ensure the user is a Marketparticipant
248-
247+
// ensure the user is a Marketparticipant
248+
//ensure!(<ApplicantsByMarketplace<T>>::get(marketplace_id, ApplicationStatus::Approved).contains(&authority), Error::<T>::ApplicantNotFound);
249249
//ensure the item is for sale, if not, return error
250250
ensure!(<OffersByItem<T>>::contains_key(collection_id, item_id), Error::<T>::ItemNotForSale);
251-
251+
252252
//ensure the marketplace exists
253253
ensure!(<Marketplaces<T>>::contains_key(marketplace_id), Error::<T>::MarketplaceNotFound);
254+
Self::is_authorized(authority.clone(), &marketplace_id,Permission::EnlistBuyOffer)?;
254255

255256
//ensure the collection exists
256257
//For this case user doesn't have to be the owner of the collection
@@ -314,6 +315,8 @@ impl<T: Config> Pallet<T> {
314315
}
315316

316317
pub fn do_take_sell_offer(buyer: T::AccountId, offer_id: [u8;32], marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId,) -> DispatchResult {
318+
ensure!(<Marketplaces<T>>::contains_key(marketplace_id), Error::<T>::MarketplaceNotFound);
319+
Self::is_authorized(buyer.clone(), &marketplace_id,Permission::TakeSellOffer)?;
317320
//This extrisicn is called by the user who wants to buy the item
318321
//ensure the collection & owner exists
319322
let owner_item = pallet_uniques::Pallet::<T>::owner(collection_id, item_id).ok_or(Error::<T>::OwnerNotFound)?;
@@ -358,6 +361,8 @@ impl<T: Config> Pallet<T> {
358361

359362
pub fn do_take_buy_offer(authority: T::AccountId, offer_id: [u8;32], marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId,) -> DispatchResult {
360363
//This extrinsic is called by the owner of the item who accepts the buy offer from the interested user.
364+
ensure!(<Marketplaces<T>>::contains_key(&marketplace_id), Error::<T>::MarketplaceNotFound);
365+
Self::is_authorized(authority.clone(), &marketplace_id,Permission::TakeBuyOffer)?;
361366
//ensure the collection & owner exists
362367
let owner_item = pallet_uniques::Pallet::<T>::owner(collection_id, item_id).ok_or(Error::<T>::OwnerNotFound)?;
363368

@@ -408,7 +413,7 @@ impl<T: Config> Pallet<T> {
408413
pub fn do_duplicate_offer(authority: T::AccountId, offer_id: [u8;32], marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId, modified_price: BalanceOf<T>) -> DispatchResult{
409414
//ensure new marketplace_id exits
410415
ensure!(<Marketplaces<T>>::contains_key(marketplace_id), Error::<T>::MarketplaceNotFound);
411-
416+
Self::is_authorized(authority.clone(), &marketplace_id,Permission::DuplicateOffer)?;
412417
//ensure that the offer_id exists
413418
ensure!(<OffersInfo<T>>::contains_key(offer_id), Error::<T>::OfferNotFound);
414419

@@ -455,7 +460,7 @@ impl<T: Config> Pallet<T> {
455460
pub fn do_remove_offer(authority: T::AccountId, offer_id: [u8;32], marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId, ) -> DispatchResult {
456461
//ensure marketplace_id exits
457462
ensure!(<Marketplaces<T>>::contains_key(marketplace_id), Error::<T>::MarketplaceNotFound);
458-
463+
Self::is_authorized(authority.clone(), &marketplace_id,Permission::RemoveOffer)?;
459464
//ensure the offer_id exists
460465
ensure!(<OffersInfo<T>>::contains_key(offer_id), Error::<T>::OfferNotFound);
461466

pallets/gated-marketplace/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ mod mock;
88
#[cfg(test)]
99
mod tests;
1010

11-
#[cfg(feature = "runtime-benchmarks")]
12-
mod benchmarking;
13-
1411
mod functions;
1512
mod types;
1613

pallets/gated-marketplace/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ parameter_types! {
153153
pub const MaxRolesPerPallet: u32 = 6;
154154
pub const RoleMaxLen: u32 = 25;
155155
pub const PermissionMaxLen: u32 = 25;
156-
pub const MaxPermissionsPerRole: u32 = 5;
156+
pub const MaxPermissionsPerRole: u32 = 11;
157157
pub const MaxRolesPerUser: u32 = 2;
158158
pub const MaxUsersPerRole: u32 = 2;
159159
}

pallets/gated-marketplace/src/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,9 @@ fn enlist_buy_offer_an_item_can_receive_multiple_buy_offers(){
10851085
let offer_id2 = GatedMarketplace::offers_by_account(2).iter().next().unwrap().clone();
10861086
assert!(GatedMarketplace::offers_info(offer_id2).is_some());
10871087

1088+
// User 3 will buy the asset so it'll have to enter the marketplace first
1089+
assert_ok!(GatedMarketplace::apply(Origin::signed(3),m_id,create_application_fields(2), None ));
1090+
assert_ok!(GatedMarketplace::enroll(Origin::signed(1), m_id , AccountOrApplication::Account(3), true, default_feedback()));
10881091
assert_ok!(GatedMarketplace::enlist_buy_offer(Origin::signed(3), m_id, 0, 0, 1200));
10891092
let offer_id3 = GatedMarketplace::offers_by_account(3).iter().next().unwrap().clone();
10901093
assert!(GatedMarketplace::offers_info(offer_id3).is_some());

pallets/gated-marketplace/src/types.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,13 @@ impl Permission{
9898

9999
pub fn admin_permissions()-> Vec<Vec<u8>>{
100100
use crate::types::Permission::*;
101-
[Enroll.to_vec(),
101+
let mut admin_permissions = [Enroll.to_vec(),
102102
AddAuth.to_vec(),
103103
RemoveAuth.to_vec(),
104104
UpdateLabel.to_vec(),
105-
RemoveMarketplace.to_vec()].to_vec()
105+
RemoveMarketplace.to_vec()].to_vec();
106+
admin_permissions.append(&mut Permission::participant_permissions());
107+
admin_permissions
106108
}
107109

108110
pub fn participant_permissions()->Vec<Vec<u8>>{

0 commit comments

Comments
 (0)