public Authority GetAuthority(UserInfo user)
 {
     var userKey = user.Key;
       var auth = _authorityByUserId.Lookup(userKey);
       if(auth == null) {
     var roles = _app.GetUserRoles(user);
     auth = GetAuthority(roles);
     _authorityByUserId.Add(userKey, auth);
       }
       user.Authority = auth;
       return auth;
 }
Exemple #2
0
 private ISecureSession OpenSecureSession(UserInfo user)
 {
     var opContext = new OperationContext(SetupHelper.BooksApp, user);
       var session = opContext.OpenSecureSession();
       return session;
 }
        public static void CreateSampleBooks(EntityApp app)
        {
            //Create identity for sample data generator; this results in SampleDataGenerator showing up in UserSession/UserTransaction tables
              // Books and coupons will reference these transactions as 'CreatedIn'
              var session = app.OpenSystemSession();
              var dataGenUser = session.NewUser("SampleDataGenerator", UserType.StoreAdmin);
              session.SaveChanges();
              var userInfo = new UserInfo(dataGenUser.Id, dataGenUser.UserName);
              var dataGenOpCtx = new OperationContext(app, userInfo);
              session = dataGenOpCtx.OpenSystemSession();
              session.EnableCache(false);

              //Publishers and authors
              var msPub = session.NewPublisher("MS Books"); //we are using extension method here
              var kidPub = session.NewPublisher("Kids Books");
              var johnBio = ConstructLongText(4000);
              var authorJohn = session.NewAuthor("John", "Sharp", johnBio);
              var authorJack = session.NewAuthor("Jack", "Pound");
              var authorJim = session.NewAuthor("Jim", "Hacker"); //this author is not user - we'll use this author to check some tricky query in tests
              var john = authorJohn.User = session.CreateUser("John", UserType.Author);

              var pubDate = DateTime.Today.AddYears(-1);
              //Books on programming from MS Books
              var csBook = session.NewBook(BookEdition.Paperback | BookEdition.EBook, BookCategory.Programming,
                 "c# Programming", "Expert programming in c#", msPub, pubDate, 20.0m);
              // Some multiline text in Abstract
              csBook.Abstract = @"Expert guide to programming in c# 4.0.
            Highly recommended for beginners and experts.
            Covers c# 4.0.";
              csBook.CoverImage = LoadImageFromResource(session, "csBookCover.jpg");
              csBook.Authors.Add(authorJohn); //this is many-to-many
              csBook.Authors.Add(authorJack);
              csBook.Editor = session.EntitySet<IUser>().First(u => u.UserName == "Linda");
              var vbBook = session.NewBook(BookEdition.Paperback | BookEdition.Hardcover, BookCategory.Programming,
                          "VB Programming", "Expert programming in VB", msPub, pubDate, 25.0m);
              vbBook.Authors.Add(authorJack);
              vbBook.CoverImage = LoadImageFromResource(session, "vbBookCover.jpg");

              //Folk tale, no authors
              var kidBook = session.NewBook(BookEdition.Hardcover, BookCategory.Kids,
               "Three little pigs", "Folk tale", kidPub, pubDate, 10.0m);
              var winBook = session.NewBook(BookEdition.Hardcover, BookCategory.Programming,
               "Windows Programming", "Introduction to Windows Programming", msPub, pubDate.AddYears(-10), 30.0m);
              winBook.Authors.Add(authorJohn);
              winBook.CoverImage = LoadImageFromResource(session, "winBookCover.jpg");
              var comicBook = session.NewBook(BookEdition.Paperback, BookCategory.Fiction, "IronMan", null, kidPub, null, 3);
              //Coupons
              var coupon1 = session.NewCoupon("C1", 10, DateTime.Now.AddMonths(1));
              var coupon2 = session.NewCoupon("C2", 10, DateTime.Now.AddMonths(1));
              var coupon3 = session.NewCoupon("C3", 10, DateTime.Now.AddMonths(1));

              try {
            session.SaveChanges();  //Save books, coupons, users and logins
              } catch(ClientFaultException ex) {
            var msgs = ex.GetMessages();
            Debug.WriteLine(msgs);
            throw;
              }

              //Orders
              var dora = session.EntitySet<IUser>().First(u => u.UserName == "Dora");
              var doraOrder = session.NewOrder(dora);
              doraOrder.Add(csBook, 1);
              doraOrder.Add(kidBook, 2);
              doraOrder.CompleteOrder("C1");
              //Create one empty order, for testing includes in queries
              var doraOrder2 = session.NewOrder(dora);
              doraOrder2.Status = OrderStatus.Canceled;

              var diego = session.EntitySet<IUser>().First(u => u.UserName == "Diego");
              var diegoOrder = session.NewOrder(diego);
              diegoOrder.Add(vbBook, 1);
              diegoOrder.Add(csBook, 1);
              diegoOrder.CompleteOrder();
              //Reviews
              var doraReview = session.NewReview(dora, csBook, 5, "Very interesting book!", "Liked it very much!");
              var diegoReview = session.NewReview(diego, vbBook, 1, "Not worth it.", "Did not like it at all.");
              // special reviews with text including wildcards for LIKE operator - will use them to test wildcard escaping in LIKE
              var duffy = session.EntitySet<IUser>().First(u => u.UserName == "Duffy");
              session.NewReview(duffy, comicBook, 1, "'Boo", "'Boo");
              session.NewReview(duffy, comicBook, 1, "_Boo", "_Boo");
              session.NewReview(duffy, comicBook, 1, "%Boo", "%Boo");
              session.NewReview(duffy, comicBook, 1, "[Boo]", "[Boo]");
              session.NewReview(duffy, comicBook, 1, "]Boo[", "]Boo[");
              session.NewReview(duffy, comicBook, 1, @"\Boo\oo", @"\Boo\oo");
              session.NewReview(duffy, comicBook, 1, @"/Boo/oo", @"/Boo/oo");

              //Save orders
              try {
            session.SaveChanges();
              } catch (ClientFaultException ex) {
            var msgs = ex.GetMessages();
            Debug.WriteLine(msgs);
            throw;
              }
        }
 public void UserLoggedOut(UserInfo user)
 {
     _authorityByUserId.Remove(user.Key);
 }
Exemple #5
0
 //Provides user roles for a given user
 public override IList<Role> GetUserRoles(UserInfo user)
 {
     switch(user.Kind) {
     case UserKind.Anonymous:
       var roles = new List<Role>();
       roles.Add(Authorization.AnonymousUser);
       return roles;
     case UserKind.AuthenticatedUser:
       var session = this.OpenSystemSession();
       var iUser = session.GetEntity<IUser>(user.UserId);
       return Authorization.GetRoles(iUser.Type);
       }
       return new List<Role>(); //never happens
 }