예제 #1
0
        public void DeassignUserToRole(string mUser, string mRole)
        {
            if (_App == null)
            {
                return;
            }
            IuserService UserSrv  = new userService(SessionFactoryConfigPath);
            IroleService RoleSrv  = new roleService(SessionFactoryConfigPath);
            user         TempUser = UserSrv.GetByName(mUser, _App.AppID);

            if (TempUser == null)
            {
                return;
            }
            role TempRole = RoleSrv.GetByName(mRole, _App.AppID);

            if (TempRole == null)
            {
                return;
            }
            if (TempUser.Roles.Contains(TempRole))
            {
                TempUser.Roles.Remove(TempRole);
            }
            UserSrv.CommitChanges();
        }
예제 #2
0
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            if (_App == null)
            {
                return;
            }
            IuserService UserSrv = new userService(SessionFactoryConfigPath);
            IroleService RoleSrv = new roleService(SessionFactoryConfigPath);

            foreach (string UN in usernames)
            {
                user mUser = UserSrv.GetByName(UN, _App.AppID);
                if (mUser != null)
                {
                    string[] currentRoles = (from r in mUser.Roles where r.AppID == _App.AppID select r.name).ToArray();
                    foreach (string r in roleNames)
                    {
                        if (!currentRoles.Contains(r))
                        {
                            role mRole = RoleSrv.GetByName(r, _App.AppID);
                            if (mRole != null)
                            {
                                mUser.Roles.Add(mRole);
                            }
                        }
                    }
                    UserSrv.Save(mUser);
                }
            }
            UserSrv.CommitChanges();
        }
예제 #3
0
        /// <summary>
        /// Removes the specified user names from the specified roles for the configured applicationName
        /// </summary>
        /// <param name="usernames"> A string array of user names to be removed from the specified roles.</param>
        /// <param name="roleNames">A string array of role names to remove the specified user names from.</param>
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            if (_App == null)
            {
                return;
            }
            IuserService UserSrv  = new userService(SessionFactoryConfigPath);
            IroleService RoleSrv  = new roleService(SessionFactoryConfigPath);
            string       hql      = "from user u where u.username in (:usernames)";
            IList <user> UserList = UserSrv.GetbyHQuery(hql, new SQLParam("usernames", usernames.ToString()));
            string       hql2     = "from role r where r.name in (:roleNames) AND r.AppID = :AppID";
            IList <role> RoleList = RoleSrv.GetbyHQuery(hql2, new SQLParam("roleNames", roleNames.ToString()), new SQLParam("AppID", _App.AppID));

            foreach (user u in UserList)
            {
                foreach (role r in RoleList)
                {
                    if (u.Roles.Contains(r))
                    {
                        u.Roles.Remove(r);
                    }
                }
            }
            UserSrv.CommitChanges();
        }
예제 #4
0
        /// <summary>
        /// remove all oldrole assign for the permission and assign new [mRoles] for the permission
        /// </summary>
        /// <param name="mObject"></param>
        /// <param name="mOperation"></param>
        /// <param name="mRoles"></param>
        public void UpdatePermission(string mObject, string mOperation, string[] mRoles)
        {
            if (_App == null)
            {
                return;
            }

            IroleService RoleSrv = new roleService(SessionFactoryConfigPath);
            string       HQL     = "from role r where r.AppID = :AppID AND r.name in ({0})";
            string       ParaStr = ":" + string.Join(",:", mRoles);

            HQL = string.Format(HQL, ParaStr);
            SQLParam[] paramList = new SQLParam[mRoles.Length + 1];
            paramList[0] = new SQLParam("AppID", _App.AppID);
            for (int i = 0; i < mRoles.Length; i++)
            {
                paramList[i + 1] = new SQLParam(mRoles[i], mRoles[i]);
            }
            //List<role> RoleLst = RoleSrv.GetbyHQuery(HQL, new SQLParam("rolenames", string.Join(",",mRoles)), new SQLParam("AppID", _App.AppID));
            List <role> RoleLst = RoleSrv.GetbyHQuery(HQL, paramList);

            if (RoleLst == null || RoleLst.Count == 0)
            {
                return;
            }
            //Grant permission
            IpermissionService PermissionSrv  = new permissionService(SessionFactoryConfigPath);
            permission         TempPermission = PermissionSrv.GetPermission(mObject, mOperation, _App.AppID);

            if (TempPermission != null)
            {
                List <role> TmpRolseLst = new List <role>();
                foreach (role r in TempPermission.Roles)
                {
                    if (!RoleLst.Contains(r))
                    {
                        TmpRolseLst.Add(r);
                    }
                }

                foreach (role r in TmpRolseLst)
                {
                    TempPermission.Roles.Remove(r);
                }

                foreach (role r in RoleLst)
                {
                    if (!TempPermission.Roles.Contains(r))
                    {
                        TempPermission.Roles.Add(r);
                    }
                }

                PermissionSrv.CommitChanges();
            }
        }
예제 #5
0
        public override bool RoleExists(string roleName)
        {
            if (_App == null)
            {
                return(false);
            }
            IroleService RoleSrv = new roleService(SessionFactoryConfigPath);

            return(RoleSrv.GetByName(roleName, _App.AppID) != null);
        }
예제 #6
0
        public void UpdateUsersToRoles(string username, string[] roleNames)
        {
            if (_App == null)
            {
                return;
            }
            IuserService UserSrv = new userService(SessionFactoryConfigPath);
            IroleService RoleSrv = new roleService(SessionFactoryConfigPath);
            user         mUser   = UserSrv.GetByName(username, _App.AppID);

            updateRolesForUser(mUser, roleNames);
        }
예제 #7
0
        public IList <operation> GetOperationsOnRoleObject(string mRole, string mObject)
        {
            if (_App == null)
            {
                return(null);
            }
            IobjectService ObjectSrv  = new objectService(SessionFactoryConfigPath);
            IroleService   RoleSrv    = new roleService(SessionFactoryConfigPath);
            role           TempRole   = RoleSrv.GetByName(mRole, _App.AppID);
            objectRbac     TempObject = ObjectSrv.GetByName(mObject, _App.AppID);

            return((from per in TempRole.Permissions where (per.ObjectRBAC == TempObject) select per.Operation).ToList <operation>());
        }
예제 #8
0
        public override string[] GetUsersInRole(string roleName)
        {
            if (_App == null)
            {
                return(null);
            }

            IroleService RoleSrv = new roleService(SessionFactoryConfigPath);
            role         mRole   = RoleSrv.GetByName(roleName, _App.AppID);

            if (mRole == null)
            {
                return(null);
            }
            return((from u in mRole.Users select u.username).ToArray());
        }
예제 #9
0
        public override string[] GetAllRoles()
        {
            if (_App == null)
            {
                return(null);
            }
            IroleService RoleSrv = new roleService(SessionFactoryConfigPath);
            IList <role> lst     = RoleSrv.GetAll(_App.AppID);

            if (lst == null || lst.Count == 0)
            {
                return new string[] { }
            }
            ;
            return((from r in lst select r.name).ToArray());
        }
예제 #10
0
        private void updateRolesForUser(user mUser, string[] roleNames)
        {
            IuserService UserSrv = new userService(SessionFactoryConfigPath);
            IroleService RoleSrv = new roleService(SessionFactoryConfigPath);

            if (mUser != null)
            {
                if (roleNames == null || roleNames.Length <= 0)
                {
                    if (mUser.Roles != null)
                    {
                        mUser.Roles.Clear();
                    }
                }
                else
                {
                    if (mUser.Roles == null)
                    {
                        mUser.Roles = new List <role>();
                    }
                    string[] currentRoles = (from r in mUser.Roles where r.AppID == _App.AppID select r.name).ToArray();
                    string[] RemoveRoles  = (from rl in currentRoles where !roleNames.Contains(rl) select rl).ToArray();
                    string[] InsertRoles  = (from rl in roleNames where !currentRoles.Contains(rl) select rl).ToArray();
                    //remove role
                    foreach (string r in RemoveRoles)
                    {
                        role mRole = RoleSrv.GetByName(r, _App.AppID);
                        if (mRole != null)
                        {
                            mUser.Roles.Remove(mRole);
                        }
                    }

                    foreach (string r in InsertRoles)
                    {
                        role mRole = RoleSrv.GetByName(r, _App.AppID);
                        if (mRole != null)
                        {
                            mUser.Roles.Add(mRole);
                        }
                    }
                }
                UserSrv.Save(mUser);
                UserSrv.CommitChanges();
            }
        }
예제 #11
0
        /// <summary>
        /// getAllPermissionName from Role
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public string[] RolePermissions(string role)
        {
            if (_App == null)
            {
                return(null);
            }

            IroleService RoleSrv  = new roleService(SessionFactoryConfigPath);
            role         TempRole = RoleSrv.GetByName(role, _App.AppID);

            if (TempRole == null)
            {
                return(null);
            }
            else
            {
                return((from per in TempRole.Permissions select per.name).ToArray());
            }
        }
예제 #12
0
        public override void CreateRole(string roleName)
        {
            IroleService RoleSrv = new roleService(SessionFactoryConfigPath);

            if (_App == null)
            {
                return;
            }
            role mRole = RoleSrv.GetByName(roleName, _App.AppID);

            if (mRole == null)
            {
                mRole       = new role();
                mRole.AppID = _App.AppID;
                mRole.name  = roleName;
                RoleSrv.CreateNew(mRole);
                RoleSrv.CommitChanges();
            }
        }
예제 #13
0
        public void RevokePermission(string mObject, string mOperation, string[] mRoles)
        {
            if (_App == null)
            {
                return;
            }
            //string HQL = "from role r where r.name in (:rolenames) AND r.AppID = :AppID";
            //List<role> RoleLst = RoleSrv.GetbyHQuery(HQL, new SQLParam("rolenames", mRoles), new SQLParam("AppID", _App.AppID));
            IroleService RoleSrv = new roleService(SessionFactoryConfigPath);
            string       HQL     = "from role r where r.AppID = :AppID AND r.name in ({0})";
            string       ParaStr = ":" + string.Join(",:", mRoles);

            HQL = string.Format(HQL, ParaStr);
            SQLParam[] paramList = new SQLParam[mRoles.Length + 1];
            paramList[0] = new SQLParam("AppID", _App.AppID);
            for (int i = 0; i < mRoles.Length; i++)
            {
                paramList[i + 1] = new SQLParam(mRoles[i], mRoles[i]);
            }
            List <role> RoleLst = RoleSrv.GetbyHQuery(HQL, paramList);

            if (RoleLst == null || RoleLst.Count == 0)
            {
                return;
            }

            IpermissionService PermissionSrv  = new permissionService(SessionFactoryConfigPath);
            permission         TempPermission = PermissionSrv.GetPermission(mObject, mOperation, _App.AppID);

            if (TempPermission != null)
            {
                foreach (role r in RoleLst)
                {
                    // not using r.Permissions because amount of roles allway is less than amount of Permissions. ->because perfomance
                    if (TempPermission.Roles.Contains(r))
                    {
                        TempPermission.Roles.Remove(r);
                    }
                }
                PermissionSrv.CommitChanges();
            }
        }
예제 #14
0
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            if (_App == null)
            {
                return(false);
            }
            IroleService RoleSrv = new roleService(SessionFactoryConfigPath);
            role         mRole   = RoleSrv.GetByName(roleName, _App.AppID);

            if (mRole == null)
            {
                return(false);
            }
            try
            {
                RoleSrv.Delete(mRole);
                RoleSrv.CommitChanges();
                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #15
0
        public void GrantPermission(string mObject, string mOperation, string[] mRoles)
        {
            if (_App == null)
            {
                return;
            }

            IroleService RoleSrv = new roleService(SessionFactoryConfigPath);
            string       HQL     = "from role r where r.AppID = :AppID AND r.name in ({0})";
            string       ParaStr = ":" + string.Join(",:", mRoles);

            HQL = string.Format(HQL, ParaStr);
            SQLParam[] paramList = new SQLParam[mRoles.Length + 1];
            paramList[0] = new SQLParam("AppID", _App.AppID);
            for (int i = 0; i < mRoles.Length; i++)
            {
                paramList[i + 1] = new SQLParam(mRoles[i], mRoles[i]);
            }
            List <role> RoleLst = RoleSrv.GetbyHQuery(HQL, paramList);

            if (RoleLst == null || RoleLst.Count == 0)
            {
                return;
            }
            IoperationService  OperationSrv  = new operationService(SessionFactoryConfigPath);
            IobjectService     ObjectSrv     = new objectService(SessionFactoryConfigPath);
            IpermissionService PermissionSrv = new permissionService(SessionFactoryConfigPath);
            //Grant permission
            permission TempPermission = PermissionSrv.GetPermission(mObject, mOperation, _App.AppID);

            if (TempPermission == null)
            {
                objectRbac tempObject    = ObjectSrv.GetByName(mObject, _App.AppID);
                operation  tempOperation = OperationSrv.GetByName(mOperation, _App.AppID);
                if (tempObject == null || tempOperation == null)
                {
                    return;
                }
                TempPermission            = new permission();
                TempPermission.AppID      = _App.AppID;
                TempPermission.name       = tempObject.name + ":" + tempOperation.name;
                TempPermission.ObjectRBAC = tempObject;
                TempPermission.Operation  = tempOperation;
                TempPermission.Roles      = new List <role>();
                foreach (role r in RoleLst)
                {
                    TempPermission.Roles.Add(r);
                }
                PermissionSrv.CreateNew(TempPermission);
                PermissionSrv.CommitChanges();
            }

            else
            {
                foreach (role r in RoleLst)
                {
                    if (!TempPermission.Roles.Contains(r))
                    {
                        TempPermission.Roles.Add(r);
                    }
                }
                PermissionSrv.CommitChanges();
            }
        }