コード例 #1
0
        public async Task <IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = new ISofAUser()
            {
                UserName = model.Email, Email = model.Email, Name = model.Name, Surname = model.Surname, City = model.City, PhoneNumber = model.PhoneNumber
            };


            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }
            //TODO Kad budes hteo email verifikaciju, odkomentarisi linije ispod
            string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

            string callbackUrl = Url.Link("DefaultApi", new { controller = "Account/ConfirmEmail", userId = user.Id, code });
            await UserManager.SendEmailAsync(user.Id, "Confirm your account", $"<p>Please confirm god damnit by smashing the link here! <a href=\" {callbackUrl}\">here</a></p>");

            return(Ok());
        }
コード例 #2
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var userManager = context.OwinContext.GetUserManager <ApplicationUserManager>();

            ISofAUser user = await userManager.FindAsync(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }
            else if (user.EmailConfirmed == false && user.ISofAUserRole == ISofAUserRole.User)
            {
                context.SetError("email_not_verified", "Haven't verified account yet");
                return;
            }

            ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
                                                                                OAuthDefaults.AuthenticationType);

            ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
                                                                                  CookieAuthenticationDefaults.AuthenticationType);

            AuthenticationProperties properties = CreateProperties(user);
            AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
        }
コード例 #3
0
 public ISofAUserDTO(ISofAUser Isauser)
 {
     Id          = Isauser.Id;
     Email       = Isauser.Email;
     Name        = Isauser.Name;
     Surname     = Isauser.Surname;
     City        = Isauser.City;
     PhoneNumber = Isauser.PhoneNumber;
 }
コード例 #4
0
        // TODO: Ova metoda je promenjena. Ranije je kao parametar primala string userName.
        public static AuthenticationProperties CreateProperties(ISofAUser user)
        {
            IDictionary <string, string> data = new Dictionary <string, string>
            {
                { "userName", user.UserName },
                { "adminOfTheater", user.AdminOfTheaterId.GetValueOrDefault().ToString() }, // dodatno
                { "iSofAUserRole", user.ISofAUserRole.ToString() },                         // dodatno
                { "iSofaUserId", user.Id },                                                 // dodatno
                { "redirect", user.EmailConfirmed ? "0" : "1" } // dodatno
            };

            return(new AuthenticationProperties(data));
        }
コード例 #5
0
        public IHttpActionResult AcceptFriendRequest([FromBody] ISofAUser senderId)
        {
            var potvrda = _friendRequestService.AcceptFriendRequest(User.Identity.GetUserId(), senderId.Id);

            if (potvrda)
            {
                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
コード例 #6
0
        public IHttpActionResult DeclineFriendRequest([FromBody] ISofAUser isauser)
        {
            string senderId = isauser.Id;
            var    potvrda  = _friendRequestService.DeclineFriendRequest(User.Identity.GetUserId(), senderId);

            if (potvrda)
            {
                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
コード例 #7
0
        public IHttpActionResult SendRequest([FromBody] ISofAUser isauser)
        {
            string Id    = isauser.Id;
            bool   salji = _friendRequestService.SendFriendRequest(Id, User.Identity.GetUserId());

            if (salji)
            {
                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
コード例 #8
0
        private ISofAUserDTO AddAdmin(int theaterId, ISofAUser user, ISofAUserRole role)
        {
            var theater = UnitOfWork.Theaters.Get(theaterId);

            if (theater != null)
            {
                user.ISofAUserRole    = role;
                user.AdminOfTheaterId = theater.TheaterId;
                UnitOfWork.Users.UpdateUser(user);
                UnitOfWork.SaveChanges();
                return(new ISofAUserDTO(user));
            }

            return(null);
        }
コード例 #9
0
        public void ChangeUserDetails(string userId, string Name, string Surname, string City, string PhoneNumber)
        {
            ISofAUser user = UnitOfWork.Users.Get(userId);

            if (!Name.Equals("") && Name != null)
            {
                user.Name = Name;
            }
            if (!Surname.Equals("") && Surname != null)
            {
                user.Surname = Surname;
            }
            if (!City.Equals("") && City != null)
            {
                user.City = City;
            }
            if (!PhoneNumber.Equals("") && PhoneNumber != null)
            {
                user.PhoneNumber = PhoneNumber;
            }
            UnitOfWork.SaveChanges();
        }
コード例 #10
0
        public async Task <IHttpActionResult> PostAsync(int theaterId, AdminBindingModel theaterAdmin)
        {
            ISofAUser user = new ISofAUser
            {
                UserName    = theaterAdmin.Email,
                Name        = theaterAdmin.Name,
                Surname     = theaterAdmin.Surname,
                Email       = theaterAdmin.Email,
                City        = theaterAdmin.City,
                PhoneNumber = theaterAdmin.PhoneNumber,
            };

            IdentityResult result = await UserManager.CreateAsync(user, "Password1!");

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            ISofAUserDTO admin = null;

            if (theaterAdmin.AdminType == 0)
            {
                admin = _adminService.AddTheaterAdmin(theaterId, user);
            }
            else if (theaterAdmin.AdminType == 1)
            {
                admin = _adminService.AddFanZoneAdmin(theaterId, user);
            }

            if (admin != null)
            {
                return(Ok(admin));
            }

            return(BadRequest());
        }
コード例 #11
0
        public async Task <IHttpActionResult> RegisterExternal()
        {
            //if (!ModelState.IsValid)
            //{
            //	return BadRequest(ModelState);
            //}

            var info = await Authentication.GetExternalLoginInfoAsync();

            //var info = await AuthenticationManager_GetExternalLoginInfoAsync_WithExternalBearer();
            if (info == null)
            {
                //return InternalServerError();
                return(BadRequest());
            }

            var user = new ISofAUser()
            {
                UserName = info.Email, Email = info.Email
            };


            IdentityResult result = await UserManager.CreateAsync(user);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            result = await UserManager.AddLoginAsync(user.Id, info.Login);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }
            return(Ok());
        }
コード例 #12
0
        public async Task <IHttpActionResult> PostSysAdminAsync(AdminBindingModel theaterAdmin)
        {
            ISofAUser user = new ISofAUser
            {
                UserName      = theaterAdmin.Email,
                Name          = theaterAdmin.Name,
                Surname       = theaterAdmin.Surname,
                Email         = theaterAdmin.Email,
                City          = theaterAdmin.City,
                PhoneNumber   = theaterAdmin.PhoneNumber,
                ISofAUserRole = ISofAUserRole.SysAdmin
            };

            IdentityResult result = await UserManager.CreateAsync(user, "Admin123!");

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }
            else
            {
                return(Ok());
            }
        }
コード例 #13
0
 public IEnumerable <ISofAUserDTO> GetUserFromSearch(ISofAUser user)
 {
     return(_userService.GetUsers(User.Identity.GetUserId(), user.Name));
 }
コード例 #14
0
 public ISofAUserDTO AddTheaterAdmin(int theaterId, ISofAUser user)
 {
     return(AddAdmin(theaterId, user, ISofAUserRole.TheaterAdmin));
 }
コード例 #15
0
 public ISofAUserDTO AddFanZoneAdmin(int theaterId, ISofAUser user)
 {
     return(AddAdmin(theaterId, user, ISofAUserRole.FanZoneAdmin));
 }
コード例 #16
0
        public async Task <IHttpActionResult> GetExternalLogin(string provider, string error = null)
        {
            if (error != null)
            {
                return(Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error)));
            }

            if (!User.Identity.IsAuthenticated)
            {
                return(new ChallengeResult(provider, this));
            }

            ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

            if (externalLogin == null)
            {
                return(InternalServerError());
            }

            if (externalLogin.LoginProvider != provider)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                return(new ChallengeResult(provider, this));
            }

            var loginInfo = new UserLoginInfo(externalLogin.LoginProvider, externalLogin.ProviderKey);

            ISofAUser user = await UserManager.FindAsync(loginInfo);

            bool hasRegistered = user != null;

            if (!hasRegistered)
            {
                var info = new UserLoginInfo(externalLogin.LoginProvider, externalLogin.ProviderKey);

                user = new ISofAUser()
                {
                    UserName = (User.Identity as ClaimsIdentity).FindFirstValue(ClaimTypes.Name), Email = (User.Identity as ClaimsIdentity).FindFirstValue(ClaimTypes.Email)
                };

                IdentityResult result = await UserManager.CreateAsync(user);

                if (!result.Succeeded)
                {
                    return(GetErrorResult(result));
                }

                result = await UserManager.AddLoginAsync(user.Id, loginInfo);

                if (!result.Succeeded)
                {
                    return(GetErrorResult(result));
                }
            }

            Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);

            ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager,
                                                                                OAuthDefaults.AuthenticationType);

            ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager,
                                                                                 CookieAuthenticationDefaults.AuthenticationType);

            AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user);

            Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);

            return(Ok());
        }
コード例 #17
0
 public bool RemoveFriend([FromBody] ISofAUser UserId)
 {
     return(_friendRequestService.RemoveFriend(User.Identity.GetUserId(), UserId.Id));
 }