static void CreateUser(string userName, RoleEntity role)
        {
            var user = new UserEntity
            {
                UserName = userName,
                PasswordHash = Security.EncodePassword(userName),
                Role = role,
                State = UserState.Saved,
            };

            user.SetMixin((UserEmployeeMixin e) => e.Employee, Database.Query<EmployeeEntity>().Single(e => e.FirstName == userName));

            user.Save();
        }//LoadUsers
        } //CSharpMigrations

        internal static void CreateRoles()
        {
            using (Transaction tr = new Transaction())
            {
                RoleEntity su = new RoleEntity() { Name = "Super user", MergeStrategy = MergeStrategy.Intersection }.Save();
                RoleEntity u = new RoleEntity() { Name = "User", MergeStrategy = MergeStrategy.Union }.Save();

                RoleEntity au = new RoleEntity()
                {
                    Name = "Advanced user",
                    Roles = new MList<Lite<RoleEntity>> { u.ToLite() },
                    MergeStrategy = MergeStrategy.Union
                }.Save();
                tr.Commit();
            }
        }
Exemple #3
0
        static void Schema_Saving(RoleEntity role)
        {
            if (!role.IsNew && role.Roles != null && role.Roles.IsGraphModified)
            {
                using (new EntityCache(EntityCacheType.ForceNew))
                {
                    EntityCache.AddFullGraph(role);

                    DirectedGraph<RoleEntity> newRoles = new DirectedGraph<RoleEntity>();

                    newRoles.Expand(role, r1 => r1.Roles.Select(a => a.Retrieve()));
                    foreach (var r in Database.RetrieveAll<RoleEntity>())
                    {
                        newRoles.Expand(r, r1 => r1.Roles.Select(a => a.Retrieve()));
                    }

                    var problems = newRoles.FeedbackEdgeSet().Edges.ToList();

                    if (problems.Count > 0)
                        throw new ApplicationException(
                            AuthMessage._0CyclesHaveBeenFoundInTheGraphOfRolesDueToTheRelationships.NiceToString().FormatWith(problems.Count) +
                            problems.ToString("\r\n"));
                }
            }
        }
        internal static void CreateUsers()
        {
            using (Transaction tr = new Transaction())
            {
                RoleEntity su = new RoleEntity() { Name = "Super user", MergeStrategy = MergeStrategy.Intersection }.Save();
                RoleEntity u = new RoleEntity() { Name = "User", MergeStrategy = MergeStrategy.Union }.Save();

                RoleEntity au = new RoleEntity()
                {
                    Name = "Advanced user",
                    Roles = new MList<Lite<RoleEntity>> { u.ToLite() },
                    MergeStrategy = MergeStrategy.Union
                }.Save();

                var employees = Database.Query<EmployeeEntity>().OrderByDescending(a => a.Notes.Length).ToList();

                using (OperationLogic.AllowSave<UserEntity>())
                    for (int i = 0; i < employees.Count; i++)
                    {
                        var employee = employees[i];
                        new UserEntity
                        {
                            UserName = employee.FirstName,
                            PasswordHash = Security.EncodePassword(employee.FirstName),
                            Role = i < 2 ? su :
                                   i < 5 ? au : u,
                            State = UserState.Saved,

                        }.SetMixin((UserEmployeeMixin e)=>e.Employee, employee).Save();
                    }

                tr.Commit();
            }
        }