Exemple #1
0
 private static void addPermission(DbSearcher ask, User user, int appId, Operation oper)
 {
     Permissions.Add(
         key: (user.UserName + oper.Name + appId),
         value: (ask.IsAllow(user.UserName, oper.Name, appId).ConvertToObject())
         );
 }
Exemple #2
0
        public DbKeeper(SecurityDB _db, DbSearcher _asker)
        {
            db = _db;
            db.Configuration.ProxyCreationEnabled = false;

            asker = _asker;
        }
Exemple #3
0
 private static void loadPermissions(DbSearcher ask, IQueryable <Operation> operations, User user, int appId)
 {
     foreach (var oper in operations)
     {
         addPermission(ask, user, appId, oper);
     }
 }
Exemple #4
0
 private static void addView(DbSearcher ask, int appId, User user)
 {
     Views.Add(
         key: (user.UserName + appId),
         value: (ask.GetAllUserViews(user.Id).ConvertToObjects())
         );
 }
Exemple #5
0
 private static void loadUserViewAndPermissions(DbSearcher ask, IQueryable <User> users, int appId, IQueryable <Operation> operations)
 {
     foreach (var user in users)
     {
         addView(ask, appId, user);
         loadPermissions(ask, operations, user, appId);
     }
 }
Exemple #6
0
        private static void loadAllData(DbSearcher ask)
        {
            var users  = ask.GetAllUsers();
            var appIds = ask.GetAllApplications().Select(x => x.Id);

            foreach (var appId in appIds)
            {
                loadUserViewAndPermissions(ask, users, appId, ask.GetAllOperations(appId));
            }
        }
Exemple #7
0
        public static void BuildCache(DbSearcher ask = null)
        {
            if (ask == null)
            {
                ask = new DbSearcher(new SecurityDB());
            }

            resetAllCaches();
            loadAllData(ask);
        }
Exemple #8
0
        public static void AddTheEssentialDataIfDeleteFromDB(this SecurityDB db)
        {
            var asker  = new DbSearcher(db);
            var keeper = new DbKeeper(db, asker);

            string superadmin           = AppSettings.SuperAdmin;
            string appName              = AppSettings.ApplicationName;
            string userManagerRoleName  = AppSettings.UserManagerRoleName;
            string rolerManagerRoleName = AppSettings.RoleManagerRoleName;
            int    appId = AppSettings.ApplicationId;


            if (asker.FindApplicationById(appId) == null)
            {
                keeper.AddApplication(appName, superadmin, appId);
            }

            Role userManager = asker.FindRole(userManagerRoleName, appId);

            if (userManager == null)
            {
                userManager = keeper.AddRole(new Role()
                {
                    Name = userManagerRoleName
                }, superadmin, appId);
            }

            string[] userOperations =
            {
                "AddUser",
                "DeleteUser",
                "AssignRoleToUser",
                "UnAssignRoleFromUser",
                "SeeUserDetails",
                "SeeAllUsers",
            };

            foreach (var p in userOperations)
            {
                var tmp = asker.FindOperation(p, appId);
                if (tmp == null)
                {
                    tmp = keeper.AddOperation(p, superadmin, appId);
                }

                keeper.AddOperationToRole(tmp.Id, userManager.Id, "", superadmin, appId);
            }

            Role roleManager = asker.FindRole(rolerManagerRoleName, appId);

            if (roleManager == null)
            {
                roleManager = keeper.AddRole(new Role()
                {
                    Name = rolerManagerRoleName
                }, superadmin, appId);
            }

            string[] roleOperations =
            {
                "AddRole",
                "DeleteRole",
                "AssignUserToRole",
                "UnAssignUserFromRole",
                "SeeRoleDetails",
                "SeeAllRoles",
                "UnAssignOperationFromRole",
                "AssignOperationToRole"
            };

            foreach (var p in roleOperations)
            {
                var tmp = asker.FindOperation(p, appId);
                if (tmp == null)
                {
                    tmp = keeper.AddOperation(p, superadmin, appId);
                }

                keeper.AddOperationToRole(tmp.Id, roleManager.Id, "", superadmin, appId);
            }

            User superAdmin = asker.FindUser(superadmin);

            if (superAdmin == null)
            {
                superAdmin = keeper.AddUserIfNotExisted(superadmin, "", "", superadmin);
            }

            keeper.AddUserToRole(superAdmin.Id, userManager.Id, superadmin);
            keeper.AddUserToRole(superAdmin.Id, roleManager.Id, superadmin);

            db.SaveChanges();
        }
Exemple #9
0
        protected BaseController()
        {
            var db = new SecurityDB();

            ask = new DbSearcher(db);
        }