示例#1
0
文件: Startup.cs 项目: jagolu/TFG-API
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDBContext context)
        {
            app.UseCors("myCORS");
            app.UseStaticFiles();
            app.UseSpaStaticFiles();
            app.UseHttpsRedirection();
            app.UseAuthentication();


            app.UseSignalR(routes =>
            {
                routes.MapHub <ChatHub>("/chatter");
                routes.MapHub <NotificationHub>("/notificatter");
            });
            app.UseMvc(routes => {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });


            EmailSender.Initialize(Configuration);
            TokenGenerator.Initialize(Configuration);
            PasswordHasher.Initialize(Configuration);
            SendNotification.Initialize(Configuration);
            KickChatNotification.Initialize(Configuration);
            DBInitializer.Initialize(context);

            app.UseSpa(spa => spa.Options.SourcePath = "webInterface");
        }
        /// <summary>
        /// Kick a user out from the group
        /// </summary>
        /// <param name="order">The info to kick a user from the group</param>
        /// See <see cref="Areas.GroupManage.Models.KickUser"/> to know the param structure
        /// <returns>The updated group page</returns>
        /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure
        public async System.Threading.Tasks.Task <IActionResult> removeUserAsync([FromBody] KickUser order)
        {
            User user = TokenUserManager.getUserFromToken(HttpContext, _context); //The user who tries to kick the user from the group

            if (!user.open)
            {
                return(BadRequest(new { error = "YoureBanned" }));
            }
            if (AdminPolicy.isAdmin(user, _context))
            {
                return(BadRequest("notAllowed"));
            }
            UserGroup targetUser = new UserGroup();
            Group     group      = new Group();

            if (!GroupAdminFuncionlities.checkFuncionality(user, ref group, order.groupName, ref targetUser, order.publicId, _context, GroupAdminFuncionality.REMOVE_USER, false))
            {
                return(BadRequest());
            }
            if (!group.open)
            {
                return(BadRequest(new { error = "GroupBanned" }));
            }

            try
            {
                _context.Entry(targetUser).Reference("User").Load();
                User sendNew      = targetUser.User;
                Guid targetUserid = targetUser.User.id;
                await QuitUserFromGroup.quitUser(targetUser, _context, _notificationHub);

                await KickChatNotification.sendKickMessageAsync(group.name, targetUser.User.publicid, _chatHub);

                InteractionManager.manageInteraction(targetUser.User, group, interactionType.KICKED, _context);

                using (var scope = _scopeFactory.CreateScope())
                {
                    var dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDBContext>();
                    group = dbContext.Group.Where(g => g.name == order.groupName).First();
                    User recv = dbContext.User.Where(u => u.id == targetUserid).First();
                    user = dbContext.User.Where(u => u.id == user.id).First();

                    Home.Util.GroupNew.launch(sendNew, group, null, Home.Models.TypeGroupNew.KICK_USER_USER, false, dbContext);
                    Home.Util.GroupNew.launch(sendNew, group, null, Home.Models.TypeGroupNew.KICK_USER_GROUP, false, dbContext);
                    await SendNotification.send(_notificationHub, group.name, recv, NotificationType.KICKED_GROUP, dbContext);

                    return(Ok(GroupPageManager.GetPage(user, group, dbContext)));
                }
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
示例#3
0
        /// <summary>
        /// Block/unblock a user from the group
        /// </summary>
        /// <param name="order">The info to block/unblock the user</param>
        /// See <see cref="Areas.GroupManage.Models.MakeAdmin_blockUser"/> to know the param structure
        /// <returns>The updated group page</returns>
        /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure
        public async Task <IActionResult> blockUser([FromBody] MakeAdmin_blockUser order)
        {
            User user = TokenUserManager.getUserFromToken(HttpContext, _context); //The user who tries to kick the user from the group

            if (!user.open)
            {
                return(BadRequest(new { error = "YoureBanned" }));
            }
            if (AdminPolicy.isAdmin(user, _context))
            {
                return(BadRequest("notAllowed"));
            }
            UserGroup targetUser = new UserGroup();
            Group     group      = new Group();

            if (!GroupAdminFuncionlities.checkFuncionality(user, ref group, order.groupName, ref targetUser, order.publicId, _context, GroupAdminFuncionality.BLOCK_USER, order.make_unmake))
            {
                return(BadRequest());
            }
            if (!group.open)
            {
                return(BadRequest(new { error = "GroupBanned" }));
            }

            try
            {
                _context.Entry(user).Collection("groups").Load();
                UserGroup callerUG = user.groups.Where(g => g.groupid == group.id).First();

                targetUser.blocked   = !targetUser.blocked;
                targetUser.role      = RoleManager.getGroupNormal(_context);
                targetUser.blockedBy = callerUG.role;
                _context.Update(targetUser);
                _context.SaveChanges();

                _context.Entry(targetUser).Reference("User").Load();
                if (order.make_unmake)
                {
                    await KickChatNotification.sendKickMessageAsync(group.name, targetUser.User.publicid, _chatHub);
                }
                await sendMessages(targetUser, group, order.make_unmake);

                return(Ok(GroupPageManager.GetPage(user, group, _context)));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }