Пример #1
0
        /// <summary>
        /// Delete selected user.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public GXUserDeleteResponse Post(GXUserDeleteRequest request)
        {
            IAuthSession s = this.GetSession(false);
            int id = Convert.ToInt32(s.Id);
            if (id == 0)
            {
                throw new ArgumentException("Remove failed. Invalid session ID.");
            }
            if (!GuruxAMI.Server.GXBasicAuthProvider.CanUserEdit(s))
            {
                throw new ArgumentException("Remove not allowed.");
            }
            List<GXEventsItem> events = new List<GXEventsItem>();
            bool superAdmin = GuruxAMI.Server.GXBasicAuthProvider.IsSuperAdmin(s);
            lock (Db)
            {
                foreach (int it in request.UserIDs)
                {
                    if (it == 0)
                    {
                        throw new ArgumentException("ID is required");
                    }
                    if (!superAdmin && !GXUserGroupService.CanAccess(Db, id, it))
                    {
                        throw new ArgumentException("Access denied.");
                    }
#if !SS4
                    GXAmiUser user = Db.QueryById<GXAmiUser>(it);
#else
                    GXAmiUser user = Db.SingleById<GXAmiUser>(it);
#endif                   
                    //Remove user from the user group.
                    if (request.GroupIDs != null && request.GroupIDs.Length != 0)
                    {
                        foreach (long gid in request.GroupIDs)
                        {
                            if (!superAdmin)
                            {
                                List<GXAmiUser> list = GetUsers(s, Db, 0, gid, false, false, null, SearchOperator.None, SearchType.All);
                                if (list.Count == 1)
                                {
                                    throw new ArgumentException("Remove not allowed.");
                                }
                            }
                            string query = string.Format("UserGroupID = {0} AND UserID = {1}", gid, it);
                            GXAmiUserGroupUser item = Db.Select<GXAmiUserGroupUser>(query)[0];
                            Db.Delete<GXAmiUserGroupUser>(item);
#if !SS4
                            GXAmiUserGroup ug = Db.QueryById<GXAmiUserGroup>(gid);
#else
                            GXAmiUserGroup ug = Db.SingleById<GXAmiUserGroup>(gid);
#endif                           
                            events.Add(new GXEventsItem(ActionTargets.User, Actions.Edit, user));
                            events.Add(new GXEventsItem(ActionTargets.User, Actions.Edit, ug));
                        }
                    }
                    else //Remove user.
                    {
                        // You can not delete yourself.
                        if (it == id)
                        {
                            throw new ArgumentException("Remove not allowed.");
                        }
                        if (request.Permanently)
                        {
                            Db.DeleteById<GXAmiUser>(it);
                        }
                        else
                        {
                            user.Removed = DateTime.Now.ToUniversalTime();
                            Db.UpdateOnly(user, p => p.Removed, p => p.Id == it);
                        }
                        events.Add(new GXEventsItem(ActionTargets.User, Actions.Remove, user));
                        //Remove all user groups of the user.
                        long[] list = GXUserGroupService.GetUserGroups(Db, it);

                        //TODO: Remove only if last user.
                        foreach (long gid in list)
                        {
#if !SS4
                            GXAmiUserGroup ug = Db.QueryById<GXAmiUserGroup>(gid);
#else
                            GXAmiUserGroup ug = Db.SingleById<GXAmiUserGroup>(gid);
#endif                           
                            if (request.Permanently)
                            {
                                Db.DeleteById<GXAmiUserGroup>(gid);
                            }
                            else
                            {
                                ug.Removed = DateTime.Now.ToUniversalTime();
                                Db.UpdateOnly(ug, p => p.Removed, p => p.Id == gid);
                            }
                            events.Add(new GXEventsItem(ActionTargets.User, Actions.Edit, user));
                            events.Add(new GXEventsItem(ActionTargets.User, Actions.Edit, ug));
                        }
                    }
                }
            }
            AppHost host = this.ResolveService<AppHost>();
            host.SetEvents(Db, this.Request, id, events);
            return new GXUserDeleteResponse();
        }
Пример #2
0
        /// <summary>
        /// Delete selected user.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public GXUserDeleteResponse Post(GXUserDeleteRequest request)
        {
            IAuthSession s  = this.GetSession(false);
            int          id = Convert.ToInt32(s.Id);

            if (id == 0)
            {
                throw new ArgumentException("Remove failed. Invalid session ID.");
            }
            if (!GuruxAMI.Server.GXBasicAuthProvider.CanUserEdit(s))
            {
                throw new ArgumentException("Remove not allowed.");
            }
            List <GXEventsItem> events = new List <GXEventsItem>();
            bool superAdmin            = GuruxAMI.Server.GXBasicAuthProvider.IsSuperAdmin(s);

            lock (Db)
            {
                foreach (int it in request.UserIDs)
                {
                    if (it == 0)
                    {
                        throw new ArgumentException("ID is required");
                    }
                    if (!superAdmin && !GXUserGroupService.CanAccess(Db, id, it))
                    {
                        throw new ArgumentException("Access denied.");
                    }
#if !SS4
                    GXAmiUser user = Db.QueryById <GXAmiUser>(it);
#else
                    GXAmiUser user = Db.SingleById <GXAmiUser>(it);
#endif
                    //Remove user from the user group.
                    if (request.GroupIDs != null && request.GroupIDs.Length != 0)
                    {
                        foreach (long gid in request.GroupIDs)
                        {
                            if (!superAdmin)
                            {
                                List <GXAmiUser> list = GetUsers(s, Db, 0, gid, false, false, null, SearchOperator.None, SearchType.All);
                                if (list.Count == 1)
                                {
                                    throw new ArgumentException("Remove not allowed.");
                                }
                            }
                            string             query = string.Format("UserGroupID = {0} AND UserID = {1}", gid, it);
                            GXAmiUserGroupUser item  = Db.Select <GXAmiUserGroupUser>(query)[0];
                            Db.Delete <GXAmiUserGroupUser>(item);
#if !SS4
                            GXAmiUserGroup ug = Db.QueryById <GXAmiUserGroup>(gid);
#else
                            GXAmiUserGroup ug = Db.SingleById <GXAmiUserGroup>(gid);
#endif
                            events.Add(new GXEventsItem(ActionTargets.User, Actions.Edit, user));
                            events.Add(new GXEventsItem(ActionTargets.User, Actions.Edit, ug));
                        }
                    }
                    else //Remove user.
                    {
                        // You can not delete yourself.
                        if (it == id)
                        {
                            throw new ArgumentException("Remove not allowed.");
                        }
                        if (request.Permanently)
                        {
                            Db.DeleteById <GXAmiUser>(it);
                        }
                        else
                        {
                            user.Removed = DateTime.Now.ToUniversalTime();
                            Db.UpdateOnly(user, p => p.Removed, p => p.Id == it);
                        }
                        events.Add(new GXEventsItem(ActionTargets.User, Actions.Remove, user));
                        //Remove all user groups of the user.
                        long[] list = GXUserGroupService.GetUserGroups(Db, it);

                        //TODO: Remove only if last user.
                        foreach (long gid in list)
                        {
#if !SS4
                            GXAmiUserGroup ug = Db.QueryById <GXAmiUserGroup>(gid);
#else
                            GXAmiUserGroup ug = Db.SingleById <GXAmiUserGroup>(gid);
#endif
                            if (request.Permanently)
                            {
                                Db.DeleteById <GXAmiUserGroup>(gid);
                            }
                            else
                            {
                                ug.Removed = DateTime.Now.ToUniversalTime();
                                Db.UpdateOnly(ug, p => p.Removed, p => p.Id == gid);
                            }
                            events.Add(new GXEventsItem(ActionTargets.User, Actions.Edit, user));
                            events.Add(new GXEventsItem(ActionTargets.User, Actions.Edit, ug));
                        }
                    }
                }
            }
            AppHost host = this.ResolveService <AppHost>();
            host.SetEvents(Db, this.Request, id, events);
            return(new GXUserDeleteResponse());
        }