public IList<IUserGroup> GetUserGroupsForUser(UserBase user)
 {
     using (Session session = new Session())
     {
         AuthorizationRepository repository = new AuthorizationRepository(session);
         return repository.GetGroupsForUser(user);
     }
 }
Esempio n. 2
0
 public IList <IUserGroup> GetUserGroupsForUser(UserBase user)
 {
     using (Session session = new Session())
     {
         AuthorizationRepository repository = new AuthorizationRepository(session);
         return(repository.GetGroupsForUser(user));
     }
 }
Esempio n. 3
0
    protected void btnAddToGroup_Click(object sender, EventArgs e)
    {
        IList<int> userIds = new List<int>();
        foreach (RepeaterItem item in this.rptAllGroup.Items)
        {
            HtmlInputCheckBox chk = item.FindControl("checkbox") as HtmlInputCheckBox;
            if (chk != null)
            {
                if (chk.Checked)
                {
                    userIds.Add(int.Parse(chk.Value));
                }
            }
        }

        if (userIds.Count > 0)
        {
            int[] arrayIds = new int[userIds.Count];
            userIds.CopyTo(arrayIds, 0);
            using (_session = new Session())
            {
                UserGroup group = Magic.Sys.UserGroup.Retrieve(_session, this._groupId);
                try
                {
                    AuthorizationRepository repository = new AuthorizationRepository(_session);

                    repository.AddUsersToGroups(arrayIds,new UserGroup[]{group});

                    LoadUserInGroup();
                }
                catch (UnauthorizedException ex)
                {
                    WebUtil.ShowMsg(this, ex.Message, "警告");
                }
                catch (ApplicationException ex)
                {
                    WebUtil.ShowMsg(this, ex.Message, "提示");
                }
                catch (Exception ex)
                {
                    logger.Info("向用户组添加用户", ex);
                    WebUtil.ShowError(this, ex);
                }
            }
        }
    }
Esempio n. 4
0
    protected void btnRemoveFromUser_Click(object sender, EventArgs e)
    {
        IList<int> groupIds = new List<int>();
        foreach (RepeaterItem item in this.rptUserToGroup.Items)
        {
            HtmlInputCheckBox chk = item.FindControl("checkbox") as HtmlInputCheckBox;
            if (chk != null && chk.Checked)
                groupIds.Add(Magic.Framework.Utils.Cast.Int(chk.Value, 0));
        }
        if (groupIds.Count > 0)
        {
            int[] arrayIds = new int[groupIds.Count];
            groupIds.CopyTo(arrayIds, 0);
            using (_session = new Session())
            {
                UserBase user = Magic.Sys.User.Retrieve(_session, this._userId);

                try
                {
                    AuthorizationRepository repository = new AuthorizationRepository(_session);
                    repository.RemoveUsersFromGroups(new UserBase[] { user }, arrayIds);
                    LoadUserToGroup();
                }
                catch (UnauthorizedException ex)
                {
                    WebUtil.ShowMsg(this, ex.Message, "警告");
                }
                catch (ApplicationException ex)
                {
                    WebUtil.ShowMsg(this, ex.Message, "提示");
                }
                catch (Exception ex)
                {
                    logger.Info("AddUsersToGroups", ex);
                    WebUtil.ShowError(this, ex);
                }
            }
        }
    }
Esempio n. 5
0
    void LoadUserInGroup()
    {
        UserGroup group = Magic.Sys.UserGroup.Retrieve(_session, _groupId);
        if (group != null)
        {
            this.txtGroupName.Text = group.Name;

            AuthorizationRepository repository = new AuthorizationRepository(_session);
            IList<UserBase> users = repository.FindUsersInGroup(group, "");
            this.rptUserInGroup.DataSource = users;
            this.rptUserInGroup.DataBind();

            DataTable dt = repository.GetAssigningUserForGroup(group.GroupId, "");
            this.rptAllGroup.DataSource = dt;
            this.rptAllGroup.DataBind();
        }
    }
Esempio n. 6
0
    void LoadUserToGroup()
    {
        User user = Magic.Sys.User.Retrieve(_session, _userId);
        if (user != null)
        {
            this.lblUserName.Text = user.FullName;

            AuthorizationRepository repository = new AuthorizationRepository(_session);
            IList<IUserGroup> usr2grps = repository.GetGroupsForUser(user);
            this.rptUserToGroup.DataSource = usr2grps;
            this.rptUserToGroup.DataBind();

            if (usr2grps == null || usr2grps.Count <= 0)
                this.rptAllGroup.DataSource = repository.GetAllGroups();
            else
                this.rptAllGroup.DataSource = repository.GetAssigningGroupsForUser(user);
            this.rptAllGroup.DataBind();
        }
    }
Esempio n. 7
0
    private static SimpleJson LoadPermissionTree()
    {
        PermissionType type = Cast.Enum<PermissionType>(WebUtil.ParamInt("type", 2));
        int uid = WebUtil.ParamInt("uid", -1);
        string name = string.Empty;
        bool existed = false;

        try
        {
            using (Session session = new Session())
            {
                if (type == PermissionType.OnUser)
                {
                    User user = User.Retrieve(session, uid);
                    if (user != null)
                    {
                        name = user.UserName;
                        existed = true;
                    }

                }
                else if (type == PermissionType.OnUserGroup)
                {
                    UserGroup group = UserGroup.Retrieve(session, uid);
                    if (group != null)
                    {
                        name = group.Name;
                        existed = true;
                    }
                }
                if (!existed)
                {
                    throw new Exception("没有选择用户群组!");
                }
                AuthorizationRepository repository = new AuthorizationRepository(session);

                DataTable dt = null;
                dt = repository.GetAssignedPermssions(type, uid);

                Operation op = Operation.GetRoot(session);
                string id = "assignedPrms";

                StringBuilder builder = new StringBuilder();
                builder.Append(@"<ul id=""" + id + @""">");
                if (op != null)
                {
                    op.LoadSubTree(session);
                    BuildAssignedPrmsTree(session, op, builder, dt);
                }
                builder.Append("</ul>");

                return new SimpleJson(3)
                    .Add("html", builder.ToString())
                    .Add("name", name)
                    .Add("id", id);
            }
        }
        catch (Exception e)
        {
            log.Error("LoadPermissionTree", e);
            return new SimpleJson().HandleError(e);
        }
    }
Esempio n. 8
0
    private static SimpleJson DeleteUserGroup()
    {
        int id = WebUtil.ParamInt("id", 0);
        try
        {
            using (Session session = new Session())
            {
                UserGroup group = UserGroup.Retrieve(session, id);
                if (group != null)
                {
                    AuthorizationRepository repository = new AuthorizationRepository(session);
                    repository.DeleteUserGroup(group, true);
                }
            }
        }
        catch (Exception e)
        {
            log.Error("DeleteUserGroup", e);
            return new SimpleJson().HandleError(e);
        }

        return null;
    }
Esempio n. 9
0
 private static SimpleJson DeleteOperation()
 {
     Operation op = new Operation();
     op.OperationId = WebUtil.ParamInt("id", 0);
     op.ParentId = WebUtil.ParamInt("parent", 0);
     using (Session session = new Session())
     {
         AuthorizationRepository repository = new AuthorizationRepository(session);
         repository.RemoveOperation(op);
     }
     return null;
 }
Esempio n. 10
0
    private static SimpleJson SaveUserGroup()
    {
        string opt = WebUtil.Param("opt");

        UserGroup group = new UserGroup();
        group.GroupId = WebUtil.ParamInt("id", 0);
        group.ParentId = WebUtil.ParamInt("parent", 0);
        group.Name = WebUtil.Param("name");
        group.Description = WebUtil.Param("desc");
        group.GroupType = (UserGroupType)Enum.Parse(typeof(UserGroupType), WebUtil.Param("type"));
        group.ModifyBy = SecuritySession.CurrentUser.UserId;
        group.ModifyTime = DateTime.Now;

        using (Session session = new Session())
        {
            AuthorizationRepository repository = new AuthorizationRepository(session);
            switch (opt)
            {
                case "create":
                    if (group.ParentId == UserGroup.Root.GroupId)
                        group.GroupLevel = (short)(UserGroup.Root.GroupLevel + 1);
                    else
                        group.GroupLevel = (short)(UserGroup.Retrieve(session, group.ParentId).GroupLevel + 1);
                    group.CreateBy = group.ModifyBy;
                    group.CreateTime = group.ModifyTime;

                    repository.CreateUserGroup(group);
                    break;
                case "update":
                    if (group.IsRoot)
                        return UserGroup.Root.ToJSON();
                    group.Update(session, "Name", "Description", "GroupType");
                    break;
            }
        }
        return group.ToJSON();
    }
Esempio n. 11
0
    private static SimpleJson SavePermissionTree()
    {
        PermissionType type = Cast.Enum<PermissionType>(WebUtil.ParamInt("type", 2));
        int uid = WebUtil.ParamInt("uid", -1);
        string removedOids = WebUtil.Param("removed");
        string assignedOids = WebUtil.Param("assigned");
        char[] sep = new char[] { ',' };
        string[] removeOidArray = removedOids.Split(sep, StringSplitOptions.RemoveEmptyEntries);
        string[] assignedOidArray = assignedOids.Split(sep, StringSplitOptions.RemoveEmptyEntries);
        int[] assignIds = WebUtil.ToIntArray(assignedOidArray);
        int[] removeIds = WebUtil.ToIntArray(removeOidArray);

        try
        {
            if ((assignIds != null && assignIds.Length > 0) || (removeIds != null && removeIds.Length > 0))
            {

                using (Session sesion = new Session())
                {
                    AuthorizationRepository repository = new AuthorizationRepository(sesion);
                    sesion.BeginTransaction();
                    try
                    {
                        if (assignIds != null && assignIds.Length > 0)
                        {
                            repository.AssignPermissions(type, new int[] { uid }, assignIds);
                        }
                        if (removeIds != null && removeIds.Length > 0)
                            repository.RemovePermission(type, new int[] { uid }, removeIds);
                        sesion.Commit();
                    }
                    catch
                    {
                        sesion.Rollback();
                        throw;
                    }
                }

            }
        }
        catch (Exception ex)
        {
            log.Error("SavePermissionTree", ex);
            return new SimpleJson().HandleError(new Exception("SavePermsTree", ex));
        }
        return new SimpleJson().Add("desc", "保存成功!");
    }