示例#1
0
        /// <summary>
        /// Recieving data from connected SQL server based on user profile meta.
        /// </summary>
        /// <param name="user">Profile that contains core meta like id, login, etc.</param>
        /// <param name="callback">Delegate that would be called after finishing of operation.
        /// Return ban information. Null if not exist or failed.</param>
        /// <returns></returns>
        public static async Task RecieveServerDataAsync(User user, Action <BanInformation> callback)
        {
            bool failed = false;

            // Init new ben info.
            BanInformation banInformation = new BanInformation()
            {
                userId = user.id // Set user's id to field for useing as where param.
            };

            // Subscribe on sql error events.
            UniformDataOperator.Sql.SqlOperatorHandler.SqlErrorOccured += SQLErrorListener;

            // Request data from server.
            await UniformDataOperator.Sql.SqlOperatorHandler.Active.SetToObjectAsync(
                typeof(BanInformation),
                Session.Current.TerminationTokenSource.Token,
                banInformation,
                new string[0],
                new string[]
            {
                "user_userid"
            });

            // Inform about finishing of operation.
            callback?.Invoke(failed ? null : banInformation);

            // Callback the will has been calling in case is error occured.
            void SQLErrorListener(object sender, string message)
            {
                // Is event target.
                if (!banInformation.Equals(sender))
                {
                    return;
                }

                // Mark operation as failed.
                failed = true;

                // Unsubscribe from event.
                UniformDataOperator.Sql.SqlOperatorHandler.SqlErrorOccured -= SQLErrorListener;
            }
        }
示例#2
0
        /// <summary>
        /// Check permition for action.
        /// </summary>
        /// <param name="user">Target user.</param>
        /// <param name="rightCode">Code of right that required for action.</param>
        /// <returns></returns>
        public static bool IsBanned(User user, string rightCode)
        {
            // Check every ban.
            for (int i = 0; i < user.bans.Count; i++)
            {
                // Get ban data.
                BanInformation banInformation = user.bans[i];

                // Skip if ban expired.
                if (!banInformation.active)
                {
                    continue;
                }

                // Validate ban and disable it if already expired.
                if (banInformation.IsExpired)
                {
                    // Disactivate ban.
                    banInformation.active = false;

                    // Update profile.
                    API.LocalUsers.SetProfileAsync(user, Application.Config.Active.UsersStorageDirectory);

                    // Skip cause already expired.
                    continue;
                }

                // Check every baned right.
                foreach (string blockedRights in banInformation.blockedRights)
                {
                    // Compare rights codes.
                    if (blockedRights == rightCode)
                    {
                        // Confirm band if equal.
                        return(true);
                    }
                }
            }

            // ban not found.
            return(false);
        }