Example #1
0
        //
        // ──────────────────────────────────────────────────────────────────────────────────
        //   :::::: P U B L I C   F U N C T I O N S : :  :   :    :     :        :          :
        // ──────────────────────────────────────────────────────────────────────────────────
        //

        /// <summary>
        /// Check if a user can do a group-admin functionality
        /// </summary>
        /// <param name="caller">The user who wants to do the funcitonality</param>
        /// <param name="group">A new group object, to save the group on it</param>
        /// <param name="groupName">The name of the group where the user wants to do the action</param>
        /// <param name="ugTarget">A new UserGroup object, to save on it</param>
        /// <param name="publicUserId">The public id of the member of the group who is goins to receive the funcionality</param>
        /// <param name="context">The database context</param>
        /// <param name="type">The id of the group-admin funcitonality</param>
        /// <param name="make_unmake">True to do the funcionality, false to undo it</param>
        /// <returns>True if the user can do the funcionality, false otherwise</returns>
        public static bool checkFuncionality(User caller, ref Group group, string groupName, ref UserGroup ugTarget, string publicUserId, ApplicationDBContext context, GroupAdminFuncionality type, bool make_unmake)
        {
            try
            {
                UserGroup ugCaller = new UserGroup();
                //The caller or the group doesnt exist, or the user is not a member of the group
                if (!UserFromGroup.isOnIt(caller.id, ref group, groupName, ref ugCaller, context))
                {
                    return(false);
                }
                if (ugCaller.blocked)
                {
                    return(false);
                }

                List <User> possibleTargets = context.User.Where(u => u.publicid == publicUserId).ToList(); //The target user

                if (possibleTargets.Count() != 1 || !UserFromGroup.isOnIt(possibleTargets.First().id, ref group, groupName, ref ugTarget, context, false))
                {
                    return(false);
                }

                context.Entry(ugCaller).Reference("role").Load();
                context.Entry(ugTarget).Reference("role").Load();
                context.Entry(ugTarget).Reference("blockedBy").Load();
                Role callerRole = ugCaller.role;
                Role targetRole = ugTarget.role;

                bool can;
                switch (type)
                {
                case GroupAdminFuncionality.MAKE_ADMIN:
                    can = hasPermissionsMakeAdmin(callerRole, targetRole, make_unmake, ugTarget.blocked, context);
                    break;

                case GroupAdminFuncionality.REMOVE_USER:
                    can = hasPermissionsKickUser(callerRole, targetRole, ugTarget.blocked, ugTarget.blocked ? ugTarget.blockedBy : new Role(), context);
                    break;

                case GroupAdminFuncionality.BLOCK_USER:
                    can = hasPermissionsBlockUser(callerRole, targetRole, ugTarget, make_unmake, context);
                    break;

                default:
                    can = false;
                    break;
                }

                return(can);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        //
        // ──────────────────────────────────────────────────────────────────────────────────
        //   :::::: P U B L I C   F U N C T I O N S : :  :   :    :     :        :          :
        // ──────────────────────────────────────────────────────────────────────────────────
        //

        /// <summary>
        /// Check if the caller can do the group-maker funcionality
        /// </summary>
        /// <param name="caller">The user who is gonna do the group-maker funcitonality</param>
        /// <param name="group">A new group object, to save the group on it</param>
        /// <param name="groupName">The name of the group where the user is going to do the action</param>
        /// <param name="type">The id of the group-maker functionality</param>
        /// <param name="_context">The database context</param>
        /// <param name="newPassword">The new password of the group (if is a manage password action)</param>
        /// <param name="oldPassword">The old password of the group (if is a manage password action)</param>
        /// <returns>True if the user can do the action, false otherwise</returns>
        public static bool checkFuncionality(User caller, ref Group group, string groupName, GroupMakerFuncionality type, ApplicationDBContext _context, string newPassword = null, string oldPassword = null)
        {
            UserGroup ugCaller = new UserGroup();

            if (!UserFromGroup.isOnIt(caller.id, ref group, groupName, ref ugCaller, _context))
            {
                return(false);
            }

            bool can;

            switch (type)
            {
            case GroupMakerFuncionality.MANAGE_PASSWORD:
                can = justCheckMaker(ugCaller, _context) && hasPermissionsManagePassword(group, newPassword, oldPassword);
                break;

            case GroupMakerFuncionality.REMOVE_GROUP:
                can = justCheckMaker(ugCaller, _context);
                break;

            case GroupMakerFuncionality.STARTCREATE_FOOTBALL_BET:
                can = justCheckMaker(ugCaller, _context);
                break;

            case GroupMakerFuncionality.MANAGEWEEKPAY:
                can = justCheckMaker(ugCaller, _context);
                break;

            default:
                can = false;
                break;
            }

            return(can);
        }