Beispiel #1
0
        /// <summary>
        /// Create a new user
        /// </summary>
        /// <param name="user">Api model object</param>
        /// <param name="userCreated">ID of current user</param>
        /// <returns></returns>
        public USER Create(User user, int userCreated = 0)
        {
            var newDbUser = new USER();

            newDbUser.FirstName = user.FirstName;
            newDbUser.LastName  = user.LastName;
            newDbUser.GROUP_ID  = user.GROUP_ID;
            newDbUser.Phone     = user.Phone;
            newDbUser.Skype     = user.Skype;
            newDbUser.Email     = user.Email;
            newDbUser.Username  = user.Username;
            if (userCreated != 0)
            {
                newDbUser.CreatedBy = userCreated;
            }
            newDbUser.CreatedAt = DateTime.Now;
            //hash the user password
            newDbUser.Hash = _hashManager.Hash(user.Password);
            db.USERs.Add(newDbUser);
            db.SaveChanges();

            try
            {
                var calendars  = new GoogleCalendar();
                var calendarId = calendars.AddCalendar(newDbUser.Email);
                calendars.AddPeopleToAcl(email: newDbUser.Email, id: calendarId, false);
                newDbUser.CalendarId = calendarId;
                db.SaveChanges();
            }
            catch
            {
                throw;
            }


            return(newDbUser);
        }
Beispiel #2
0
        public HttpResponseMessage Login([FromBody] LoginApiModel apiModel)
        {
            HttpResponseMessage response = new HttpResponseMessage();

            ResponseFormat responseData;

            if (apiModel == null)
            {
                response.StatusCode  = HttpStatusCode.BadRequest;
                responseData         = ResponseFormat.Fail;
                responseData.message = ErrorMessages.INVALID_BODY;
            }
            else
            {
                var validate = _userService.ValidatePassword(apiModel.email, apiModel.password);
                if (validate.Item1 == true)
                {
                    var dbUser = _userRepository.GetByEmail(validate.Item3.Email);
                    //generate jwt token
                    var JwtToken = JwtTokenManager.GenerateJwtToken(validate.Item3);
                    //generate refresh token
                    var           RefreshToken    = JwtTokenManager.GenerateRefreshToken();
                    REFRESH_TOKEN newRefreshToken = new REFRESH_TOKEN();
                    newRefreshToken.USER_ID = dbUser.ID;
                    newRefreshToken.Token   = RefreshToken;
                    db.REFRESH_TOKEN.Add(newRefreshToken);
                    db.SaveChanges();

                    //set refresh token to httponly and add it to cookies
                    //var nv = new NameValueCollection();
                    //nv["refreshToken"] = RefreshToken;
                    //nv["seriesIdentifier"] = dbUser.ID.ToString();
                    //nv["tokenIdentifier"] = newRefreshToken.ID.ToString();
                    response.Headers.Add("set-cookie", $"refreshTokenData=refreshToken={RefreshToken}&seriesIdentifier={dbUser.ID}&tokenIdentifier={newRefreshToken.ID}; path=/; SameSite=None; Secure; max-age=2592000");

                    //create response data
                    responseData = ResponseFormat.Success;
                    if (dbUser.Avatar != null)
                    {
                        responseData.data = new
                        {
                            user = new
                            {
                                id        = validate.Item3.ID,
                                username  = validate.Item3.Username,
                                firstName = validate.Item3.FirstName,
                                lastName  = validate.Item3.LastName,
                                jwt       = JwtToken,
                                group     = dbUser.GROUP.ID,
                                avatar    = $"{StaticStrings.ServerHost}avatar?fileName={dbUser.Avatar}"
                            }
                        };
                    }
                    else
                    {
                        responseData.data = new
                        {
                            user = new
                            {
                                id        = validate.Item3.ID,
                                username  = validate.Item3.Username,
                                firstName = validate.Item3.FirstName,
                                lastName  = validate.Item3.LastName,
                                jwt       = JwtToken,
                                group     = dbUser.GROUP.ID,
                                avatar    = ""
                            }
                        };
                    }

                    if (string.IsNullOrEmpty(dbUser.CalendarId))
                    {
                        try
                        {
                            var calId = googleCalendar.AddCalendar(dbUser.Email);
                            googleCalendar.AddPeopleToAcl(dbUser.Email, calId, true);
                            _userService.UpdateCalendarId(dbUser.Email, calId);
                        }
                        catch
                        {
                        }
                    }
                    response.StatusCode = HttpStatusCode.OK;
                }
                else
                {
                    response.StatusCode  = HttpStatusCode.Unauthorized;
                    responseData         = ResponseFormat.Fail;
                    responseData.message = validate.Item2;
                }
            }
            var json = JsonConvert.SerializeObject(responseData);

            response.Content = new StringContent(json, Encoding.UTF8, "application/json");
            return(response);
        }