Пример #1
0
        private IList <BanBE> Bans_GetBansInternal(string where)
        {
            List <BanBE> bans = new List <BanBE>();

            Catalog.NewQuery(string.Format(@" /* BanningDA::GetBans */
select b.*,
	( select group_concat(bi.banip_ipaddress SEPARATOR '\n')
	  from banips bi
	  where bi.banip_ban_id = b.ban_id
	  group by bi.banip_ban_id
	) as ban_addresses,
	( select cast(group_concat(bu.banuser_user_id SEPARATOR '\n') as char)
	  from banusers bu
	  where bu.banuser_ban_id = b.ban_id
	  group by bu.banuser_ban_id
	) as ban_users
from bans b
{0}
order by b.ban_last_edit desc;", where))
            .Execute(delegate(IDataReader dr) {
                while (dr.Read())
                {
                    BanBE b = Bans_Populate(dr);
                    bans.Add(b);
                }
            });

            return(bans);
        }
Пример #2
0
        private static XDoc AppendBanXml(XDoc doc, BanBE ban)
        {
            UserBE createdBy = UserBL.GetUserById(ban.ByUserId);

            doc.Attr("id", ban.Id);
            doc.Attr("href", DekiContext.Current.ApiUri.At("site", "bans", ban.Id.ToString()));
            if (createdBy != null)
            {
                doc.Add(UserBL.GetUserXml(createdBy, "createdby", Utils.ShowPrivateUserInfo(createdBy)));
            }
            doc.Elem("date.modified", ban.LastEdit);
            doc.Elem("description", ban.Reason);
            doc.Elem("date.expires", ban.Expires);
            doc.Add(PermissionsBL.GetPermissionXml(ban.RevokeMask, "revoked"));
            doc.Start("ban.addresses");
            if (ban.BanAddresses != null)
            {
                foreach (string address in ban.BanAddresses)
                {
                    doc.Elem("address", address);
                }
            }
            doc.End();
            doc.Start("ban.users");
            if (ban.BanUserIds != null)
            {
                var banUsers = DbUtils.CurrentSession.Users_GetByIds(ban.BanUserIds);
                foreach (UserBE u in banUsers)
                {
                    doc.Add(UserBL.GetUserXml(u, null, Utils.ShowPrivateUserInfo(createdBy)));
                }
            }
            doc.End();
            return(doc);
        }
Пример #3
0
        private static BanBE ReadBanXml(XDoc doc)
        {
            BanBE b = new BanBE();

            b.BanAddresses = new List <string>();
            b.BanUserIds   = new List <uint>();
            try {
                b.Reason     = doc["description"].AsText;
                b.RevokeMask = PermissionsBL.MaskFromPermissionList(PermissionsBL.PermissionListFromString(doc["permissions.revoked/operations"].AsText ?? string.Empty));
                b.LastEdit   = DateTime.UtcNow;
                b.Expires    = doc["date.expires"].AsDate;
                b.ByUserId   = DekiContext.Current.User.ID;
                foreach (XDoc val in doc["ban.addresses/address"])
                {
                    if (!val.IsEmpty)
                    {
                        b.BanAddresses.Add(val.AsText);
                    }
                }
                foreach (XDoc val in doc["ban.users/user"])
                {
                    uint?id = val["@id"].AsUInt;
                    if (id != null)
                    {
                        b.BanUserIds.Add(id ?? 0);
                    }
                }
            } catch (ResourcedMindTouchException) {
                throw;
            } catch (Exception x) {
                throw new MindTouchInvalidOperationException(x.Message, x);
            }
            return(b);
        }
Пример #4
0
 private void BanEvent(DateTime eventTime, BanBE ban, params string[] channelPath)
 {
     try {
         XUri channel = _channel.At(BAN).At(channelPath);
         XDoc doc     = new XDoc("deki-event")
                        .Elem("channel", channel)
                        .Elem("banid", ban.Id)
                        .Elem("reason", ban.Reason)
                        .Elem("by", ban.ByUserId);
         if (ban.BanAddresses.Count > 0)
         {
             doc.Start("addresses");
             foreach (string address in ban.BanAddresses)
             {
                 doc.Elem("address", address);
             }
             doc.End();
         }
         if (ban.BanUserIds.Count > 0)
         {
             doc.Start("users");
             foreach (uint userId in ban.BanUserIds)
             {
                 doc.Start("user").Attr("id", userId).End();
             }
             doc.End();
         }
         Queue(eventTime, channel, null, new string[] { string.Format("http://{0}/deki", _wikiid) }, doc);
     } catch (Exception e) {
         _log.WarnExceptionMethodCall(e, "BanEvent", "event couldn't be created");
     }
 }
Пример #5
0
 public static void DeleteBan(BanBE ban)
 {
     if (ban != null)
     {
         DbUtils.CurrentSession.Bans_Delete(ban.Id);
         TokenReset();
     }
 }
Пример #6
0
        public Yield GetBan(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            PermissionsBL.CheckUserAllowed(DekiContext.Current.User, Permissions.ADMIN);
            BanBE ban = GetBanFromRequest(context, context.GetParam <uint>("banid"));

            response.Return(DreamMessage.Ok(BanningBL.GetBanXml(ban)));
            yield break;
        }
Пример #7
0
        public Yield PostBans(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            PermissionsBL.CheckUserAllowed(DekiContext.Current.User, Permissions.ADMIN);
            BanBE ban = BanningBL.SaveBan(request.ToDocument());

            DekiContext.Current.Instance.EventSink.BanCreated(DekiContext.Current.Now, ban);
            response.Return(DreamMessage.Ok(BanningBL.GetBanXml(ban)));
            yield break;
        }
Пример #8
0
        private BanBE GetBanFromRequest(DreamContext context, uint banid)
        {
            BanBE ban = BanningBL.GetById(banid);

            if (ban == null)
            {
                throw new BanIdNotFoundException(banid);
            }
            return(ban);
        }
Пример #9
0
        public Yield DeleteBan(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            PermissionsBL.CheckUserAllowed(DekiContext.Current.User, Permissions.ADMIN);
            BanBE ban = GetBanFromRequest(context, context.GetParam <uint>("banid"));

            BanningBL.DeleteBan(ban);
            DekiContext.Current.Instance.EventSink.BanRemoved(DekiContext.Current.Now, ban);
            response.Return(DreamMessage.Ok());
            yield break;
        }
Пример #10
0
        private BanBE Bans_Populate(IDataReader dr)
        {
            BanBE ban = new BanBE();

            ban._BanAddresses = dr.Read <string>("ban_addresses");
            ban._BanUserIds   = dr.Read <string>("ban_users");
            ban.ByUserId      = dr.Read <uint>("ban_by_user_id");
            ban.Expires       = dr.Read <DateTime?>("ban_expires", DateTime.MaxValue);
            ban.Id            = dr.Read <uint>("ban_id");
            ban.LastEdit      = dr.Read <DateTime>("ban_last_edit", DateTime.MinValue);
            ban.Reason        = dr.Read <string>("ban_reason");
            ban.RevokeMask    = dr.Read <ulong>("ban_revokemask");
            return(ban);
        }
Пример #11
0
        public uint Bans_Insert(BanBE ban)
        {
            //build banusers insert query
            StringBuilder userIdInsertQuery = new StringBuilder();

            if (ban.BanUserIds != null && ban.BanUserIds.Count > 0)
            {
                userIdInsertQuery.Append("insert into banusers (banuser_user_id, banuser_ban_id) values ");
                for (int i = 0; i < ban.BanUserIds.Count; i++)
                {
                    userIdInsertQuery.AppendFormat("{0}({1}, @banid)", i > 0 ? "," : string.Empty, ban.BanUserIds[i]);
                }

                userIdInsertQuery.Append(";");
            }

            //build banips insert query
            StringBuilder addressesInsertQuery = new StringBuilder();

            if (ban.BanAddresses != null && ban.BanAddresses.Count > 0)
            {
                addressesInsertQuery.Append("insert into banips (banip_ipaddress, banip_ban_id) values ");
                for (int i = 0; i < ban.BanAddresses.Count; i++)
                {
                    addressesInsertQuery.AppendFormat("{0}('{1}', @banid)", i > 0 ? "," : string.Empty, DataCommand.MakeSqlSafe(ban.BanAddresses[i]));
                }

                addressesInsertQuery.Append(";");
            }

            string query = string.Format(@" /*  Bans_Insert */
insert into bans (ban_by_user_id, ban_expires, ban_reason, ban_revokemask, ban_last_edit)
values(?BAN_BY_USER_ID, ?BAN_EXPIRES, ?BAN_REASON, ?BAN_REVOKEMASK, ?BAN_LAST_EDIT);
select LAST_INSERT_ID();
select LAST_INSERT_ID() into @banid;
{0}
{1}", userIdInsertQuery, addressesInsertQuery);

            return(uint.Parse(Catalog.NewQuery(query)
                              .With("BAN_BY_USER_ID", ban.ByUserId)
                              .With("BAN_EXPIRES", ban.Expires)
                              .With("BAN_REASON", ban.Reason)
                              .With("BAN_REVOKEMASK", ban.RevokeMask)
                              .With("BAN_LAST_EDIT", ban.LastEdit)
                              .Read()));
        }
Пример #12
0
        public static BanBE SaveBan(XDoc doc)
        {
            BanBE ban = ReadBanXml(doc);

            if (ArrayUtil.IsNullOrEmpty(ban.BanAddresses) && ArrayUtil.IsNullOrEmpty(ban.BanUserIds))
            {
                throw new BanEmptyInvalidArgumentException();
            }
            foreach (var address in ban.BanAddresses)
            {
                try {
                    if (IPAddress.IsLoopback(IPAddress.Parse(address)))
                    {
                        throw new LoopbackIPAddressException(address);
                    }
                } catch (FormatException) {
                } catch (ArgumentNullException) {
                    throw new InvalidIPAddressException(address);
                }
            }
            if (ban.RevokeMask == 0)
            {
                throw new BanNoPermsInvalidArgumentException();
            }

            ulong?siteOwner = DekiContext.Current.LicenseManager.GetSiteOwnerUserId();

            if (!ArrayUtil.IsNullOrEmpty(ban.BanUserIds) && (siteOwner != null) && ban.BanUserIds.Contains((uint)siteOwner))
            {
                throw new BanningOwnerConflict();
            }

            TokenReset();
            uint banId = DbUtils.CurrentSession.Bans_Insert(ban);

            if (banId == 0)
            {
                return(null);
            }
            ban.Id = banId;
            return(ban);
        }
Пример #13
0
        public uint Bans_Insert(BanBE ban) {

            //build banusers insert query
            StringBuilder userIdInsertQuery = new StringBuilder();
            if (ban.BanUserIds != null && ban.BanUserIds.Count > 0) {
                userIdInsertQuery.Append("insert into banusers (banuser_user_id, banuser_ban_id) values ");
                for (int i = 0; i < ban.BanUserIds.Count; i++) {
                    userIdInsertQuery.AppendFormat("{0}({1}, @banid)", i > 0 ? "," : string.Empty, ban.BanUserIds[i]);
                }

                userIdInsertQuery.Append(";");
            }

            //build banips insert query
            StringBuilder addressesInsertQuery = new StringBuilder();
            if (ban.BanAddresses != null && ban.BanAddresses.Count > 0) {
                addressesInsertQuery.Append("insert into banips (banip_ipaddress, banip_ban_id) values ");
                for (int i = 0; i < ban.BanAddresses.Count; i++) {
                    addressesInsertQuery.AppendFormat("{0}('{1}', @banid)", i > 0 ? "," : string.Empty, DataCommand.MakeSqlSafe(ban.BanAddresses[i]));
                }

                addressesInsertQuery.Append(";");
            }

            string query = string.Format(@" /*  Bans_Insert */
insert into bans (ban_by_user_id, ban_expires, ban_reason, ban_revokemask, ban_last_edit)
values(?BAN_BY_USER_ID, ?BAN_EXPIRES, ?BAN_REASON, ?BAN_REVOKEMASK, ?BAN_LAST_EDIT);
select LAST_INSERT_ID();
select LAST_INSERT_ID() into @banid;
{0}
{1}", userIdInsertQuery, addressesInsertQuery);

            return uint.Parse(Catalog.NewQuery(query)
            .With("BAN_BY_USER_ID", ban.ByUserId)
            .With("BAN_EXPIRES", ban.Expires)
            .With("BAN_REASON", ban.Reason)
            .With("BAN_REVOKEMASK", ban.RevokeMask)
            .With("BAN_LAST_EDIT", ban.LastEdit)
            .Read());
        }
Пример #14
0
 private BanBE Bans_Populate(IDataReader dr) {
     BanBE ban = new BanBE();
     ban._BanAddresses = dr.Read<string>("ban_addresses");
     ban._BanUserIds = dr.Read<string>("ban_users");
     ban.ByUserId = dr.Read<uint>("ban_by_user_id");
     ban.Expires = dr.Read<DateTime?>("ban_expires", DateTime.MaxValue);
     ban.Id = dr.Read<uint>("ban_id");
     ban.LastEdit = dr.Read<DateTime>("ban_last_edit", DateTime.MinValue);
     ban.Reason = dr.Read<string>("ban_reason");
     ban.RevokeMask = dr.Read<ulong>("ban_revokemask");
     return ban;
 }
Пример #15
0
 public void BanCreated(DateTime eventTime, BanBE ban)
 {
     BanEvent(eventTime, ban, CREATE);
 }
Пример #16
0
 public static XDoc GetBanXml(BanBE ban)
 {
     return(AppendBanXml(new XDoc("ban"), ban));
 }
Пример #17
0
 public void BanRemoved(DateTime eventTime, BanBE ban)
 {
     BanEvent(eventTime, ban, DELETE);
 }