Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server side Validations and security in breeze.js

I’m trying save some entities using breeze.js. Breeze is working fine and it saves all the changes as required. However, I have trouble validating and ensuring authorization is the server side. From what I’ve gather so far I guess the only way to do this is via examining the JObject passed into save bundles and constructing corresponding objects on the server side. I have to do this (instead of relying Breeze.SaveChanges as I have some logic on the server side). How do I do this? And how do I construct the Breeze.WebApi. SaveResult? Idea of any other way of solving this problem is also very welcome 

like image 982
Amila De Silva Avatar asked Mar 06 '26 21:03

Amila De Silva


1 Answers

This should be done by implementing a custom EFContextProvider.

The code below implements a custom EFContextProvider for the Northwind database and was taken directly from the documentation on the breeze.com website .

    public class NorthwindContextProvider: EFContextProvider<NorthwindIBContext>  {
    public NorthwindContextProvider() : base() { }

    protected override bool BeforeSaveEntity(EntityInfo entityInfo) {
      // return false if we don’t want the entity saved.
      // prohibit any additions of entities of type 'Role'
      if (entityInfo.Entity.GetType() == typeof(Role)
        && entityInfo.EntityState == EntityState.Added) {
        return false;
      } else {
        return true;
      }
   }

    protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap) {
      // return a map of those entities we want saved.
      return saveMap;
    }
  }
like image 77
Jacques Snyman Avatar answered Mar 10 '26 11:03

Jacques Snyman