//Path: /api/User/UpdateUser
        public HttpResponseMessage UpdateUser(UpdateUserModel updateUserModelRequest)
        {
            File.AppendAllText(SuiteWrapper.traceLogPath, Environment.NewLine + Environment.NewLine);
            SuiteWrapper.WriteTraceLog("Called 'UpdateUser' with request :" + JsonConvert.SerializeObject(updateUserModelRequest));

            string outputMessage = string.Empty;

            if (!SuiteWrapper.ValidateRequest(updateUserModelRequest, out outputMessage))
            {
                //Trace Log
                SuiteWrapper.WriteTraceLog("Exception while validating request for " + JsonConvert.SerializeObject(updateUserModelRequest) + " is : " + outputMessage);
                return(Request.CreateResponse(HttpStatusCode.NotAcceptable, outputMessage));;
            }

            try
            {
                using (PundolesEntities context = new PundolesEntities())
                {
                    var UpdEntity = context.users.AsNoTracking().FirstOrDefault(m => m.id == updateUserModelRequest.id);
                    if (UpdEntity == null)
                    {
                        SuiteWrapper.WriteTraceLog("User doesn't exist.");
                        return(Request.CreateResponse(HttpStatusCode.NotAcceptable, "User doesn't exist."));;
                    }

                    UpdEntity.user_name = updateUserModelRequest.user_name;
                    if (!string.IsNullOrEmpty(updateUserModelRequest.user_hash))
                    {
                        UpdEntity.user_hash = SuiteWrapper.CreateMD5(updateUserModelRequest.user_hash);
                    }

                    UpdEntity.first_name      = updateUserModelRequest.first_name;
                    UpdEntity.last_name       = updateUserModelRequest.last_name;
                    UpdEntity.phone_home      = updateUserModelRequest.phone_home;
                    UpdEntity.phone_mobile    = updateUserModelRequest.phone_mobile;
                    UpdEntity.department      = updateUserModelRequest.department;
                    UpdEntity.report_to_id    = updateUserModelRequest.report_to_id;
                    UpdEntity.primary_email   = updateUserModelRequest.primary_email;
                    UpdEntity.alternate_email = updateUserModelRequest.alternate_email;
                    UpdEntity.user_type       = updateUserModelRequest.user_type;
                    UpdEntity.user_status     = updateUserModelRequest.user_status;
                    UpdEntity.modified_date   = DateTime.Now;
                    UpdEntity.modifiedby_id   = updateUserModelRequest.modifiedby_id;

                    context.Entry(UpdEntity).State = EntityState.Modified;

                    context.SaveChanges();

                    SuiteWrapper.WriteTraceLog("User is Successfully updated.");
                    return(Request.CreateResponse(HttpStatusCode.OK, "Success"));
                }
            }
            catch (Exception ex)
            {
                SuiteWrapper.WriteTraceLog("Exception while updating the contact is : " + ex.Message.ToString());
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message.ToString()));;
            }
        }
        //Path: /api/User/CreateUser
        public HttpResponseMessage CreateUser(CreateUserModel CreateUserModelRequest)
        {
            // Trace Log
            File.AppendAllText(SuiteWrapper.traceLogPath, Environment.NewLine + Environment.NewLine);
            SuiteWrapper.WriteTraceLog("Called 'CreateUser' with request :" + JsonConvert.SerializeObject(CreateUserModelRequest));
            PushUserResponse pushUserResponse = new PushUserResponse();

            string outputMessage = string.Empty;

            if (!SuiteWrapper.ValidateRequest(CreateUserModelRequest, out outputMessage))
            {
                //Trace Log
                SuiteWrapper.WriteTraceLog("Exception while validating request for " + JsonConvert.SerializeObject(CreateUserModelRequest) + " is : " + outputMessage);
                return(Request.CreateResponse(HttpStatusCode.NotAcceptable, outputMessage));;
            }

            try
            {
                using (PundolesEntities context = new PundolesEntities())
                {
                    user userObject = new user();
                    userObject.user_name       = CreateUserModelRequest.user_name;
                    userObject.user_hash       = SuiteWrapper.CreateMD5(CreateUserModelRequest.user_hash);
                    userObject.first_name      = CreateUserModelRequest.first_name;
                    userObject.last_name       = CreateUserModelRequest.last_name;
                    userObject.phone_home      = CreateUserModelRequest.phone_home;
                    userObject.phone_mobile    = CreateUserModelRequest.phone_mobile;
                    userObject.department      = CreateUserModelRequest.department;
                    userObject.report_to_id    = CreateUserModelRequest.report_to_id;
                    userObject.primary_email   = CreateUserModelRequest.primary_email;
                    userObject.alternate_email = CreateUserModelRequest.alternate_email;
                    userObject.user_type       = CreateUserModelRequest.user_type;
                    userObject.user_status     = CreateUserModelRequest.user_status;
                    userObject.created_date    = DateTime.Now;
                    userObject.modified_date   = DateTime.Now;
                    userObject.createdby_id    = CreateUserModelRequest.createdby_id;
                    userObject.modifiedby_id   = CreateUserModelRequest.createdby_id;
                    context.users.Add(userObject);

                    context.SaveChanges();

                    pushUserResponse.id     = userObject.id;
                    pushUserResponse.status = "Success";

                    SuiteWrapper.WriteTraceLog("user is successfully created with response :" + JsonConvert.SerializeObject(pushUserResponse));
                    return(Request.CreateResponse(HttpStatusCode.OK, pushUserResponse));
                }
            }
            catch (Exception ex)
            {
                pushUserResponse.id     = null;
                pushUserResponse.status = ex.Message.ToString();

                SuiteWrapper.WriteTraceLog("Exception while creating user is : " + ex.Message.ToString());
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, JsonConvert.SerializeObject(pushUserResponse)));
            }
        }
Ejemplo n.º 3
0
        //public ApplicationOAuthProvider(string publicClientId)
        //{
        //    if (publicClientId == null)
        //    {
        //        throw new ArgumentNullException("publicClientId");
        //    }

        //    _publicClientId = publicClientId;
        //}

        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var allowedOrigin = context.OwinContext.Get <string>("as:clientAllowedOrigin");

            if (allowedOrigin == null)
            {
                allowedOrigin = "*";
            }
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });

            UserModel userModel = new UserModel();
            var       userName  = context.UserName;
            var       password  = context.Password;

            if (string.IsNullOrEmpty(context.UserName) || string.IsNullOrEmpty(password))
            {
                context.SetError("InvalidCredentials", "User Name and Password is mandatory.");
                return;
            }
            // Decrypty the password here
            var encryptPassword = SuiteWrapper.CreateMD5(password);
            var userManagement  = userModel.GetUser(userName, encryptPassword);

            if (userManagement != null)
            {
                if (userManagement.user_status == "Inactive")
                {
                    context.SetError("deactivate", "Account is inactive");
                    return;
                }

                var identityNewUser = new ClaimsIdentity(context.Options.AuthenticationType);
                identityNewUser.AddClaim(new Claim("id", Convert.ToString(userManagement.id)));
                var propsInvalid = new AuthenticationProperties(new Dictionary <string, string>
                {
                    {
                        "id", Convert.ToString(userManagement.id)
                    },
                    {
                        "user_name", Convert.ToString(userManagement.user_name)
                    },
                    {
                        "fullname", userManagement.first_name + " " + userManagement.last_name
                    },
                    {
                        "status", userManagement.user_status
                    },
                    {
                        "user_type", userManagement.user_type
                    }
                });
                var ticketInvalid = new AuthenticationTicket(identityNewUser, propsInvalid);
                context.Validated(ticketInvalid);
                return;
            }
            else
            {
                context.SetError("InvalidCredentials", "Invalid Username and Password.");
                return;
            }
        }