Пример #1
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", "保存成功!");
    }