Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Threading.Tasks;
- using Dapper;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using FmsDashboard.Helper;
- using FmsDashboard.Models.Cpanel;
- using FmsDashboard.Models.Cpanel.User;
- using static FmsDashboard.Helper.JqueryDataTableModel;
- using FmsDashboard.Models.Login;
- namespace FmsDashboard.Controllers
- {
- [Authorize]
- public class CpanelApiController : Controller// CPANEL API
- {
- private readonly string connectionstring = "";
- private readonly UserManagerAccessor userIdentity;
- readonly IHttpContextAccessor _httpContextAccessor;
- public CpanelApiController(IConfiguration config, IHttpContextAccessor httpContextAccessor)
- {
- connectionstring = config["ConnectionStrings:connStringEasygo"];
- _httpContextAccessor = httpContextAccessor;
- userIdentity = new UserManagerAccessor(httpContextAccessor);
- }
- public IActionResult Index()
- {
- return View();
- }
- public async Task<IActionResult> GetListUser(DtParameters model)
- {
- var user_identity = new UserManagerAccessor(_httpContextAccessor);
- var conn = new SqlConnection(connectionstring);
- var _companyId = user_identity.GetCompanyId();
- var userid = user_identity.GetUserId();
- try
- {
- IEnumerable<ListUser> result;
- var draw = model.Draw == 0 ? 1 : model.Draw;
- var start = model.Start == 0 ? 1 : model.Start;
- var length = model.Length == 0 ? 10 : model.Length;
- var search = string.IsNullOrEmpty(model.Search.Value) ? "" : model.Search.Value.ToString();
- var order = model.SortOrder;
- if (order.ToLower() == "rowno")
- order = "ul.user_nm";
- if (conn.State == ConnectionState.Closed)
- {
- conn.Open();
- }
- var p = new DynamicParameters();
- p.Add("@StartCurrent", start, dbType: DbType.Int32, direction: ParameterDirection.Input);
- p.Add("@PageSize", length, dbType: DbType.Int32, direction: ParameterDirection.Input);
- p.Add("@SearchPhrase", search, dbType: DbType.String, direction: ParameterDirection.Input);
- p.Add("@SortOrder", order, dbType: DbType.String, direction: ParameterDirection.Input);
- p.Add("@UserId", userid, dbType: DbType.Int64, direction: ParameterDirection.Input);
- p.Add("@CompanyId", _companyId, dbType: DbType.Int64, direction: ParameterDirection.Input);
- p.Add("@RecordCount", dbType: DbType.Int64, direction: ParameterDirection.Output);
- result = await SqlMapper.QueryAsync<ListUser>(conn, "usp_cpanel_list_user", p, commandType: CommandType.StoredProcedure);
- var recordsTotal = p.Get<Int64>("@RecordCount");
- var jsonData = new DtResults<ListUser>
- {
- draw = model.Draw,
- recordsTotal = recordsTotal,
- recordsFiltered = recordsTotal,
- data = result.ToList()
- };
- return Json(jsonData);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- }
- conn.Dispose();
- }
- }
- public async Task<IActionResult> CreateUser(string user_id, string user_nm, string password, long paket_id, bool allow_cmd, int only_do)
- {
- try
- {
- var user_identity = new UserManagerAccessor(_httpContextAccessor);
- var conn = new SqlConnection(connectionstring);
- var companyId = user_identity.GetCompanyId();
- var userid = user_identity.GetUserId();
- var code_status = 0;
- var message = "";
- try
- {
- if (conn.State == ConnectionState.Closed)
- {
- conn.Open();
- }
- var p = new DynamicParameters();
- p.Add("@user_id", user_id, dbType: DbType.String, direction: ParameterDirection.Input);
- p.Add("@user_nm", user_nm, dbType: DbType.String, direction: ParameterDirection.Input);
- p.Add("@password", password, dbType: DbType.String, direction: ParameterDirection.Input);
- p.Add("@paket_id", paket_id, dbType: DbType.Int64, direction: ParameterDirection.Input);
- p.Add("@allow_cmd", allow_cmd, dbType: DbType.Boolean, direction: ParameterDirection.Input);
- p.Add("@only_do", only_do, dbType: DbType.Int32, direction: ParameterDirection.Input);
- //p.Add("@userid", userid, dbType: DbType.Int64, direction: ParameterDirection.Input);
- p.Add("@companyid", companyId, dbType: DbType.Int64, direction: ParameterDirection.Input);
- var result = await SqlMapper.QueryFirstOrDefaultAsync<string>(conn, "usp_cpanel_create_user", p, commandType: CommandType.StoredProcedure);
- if (string.IsNullOrEmpty(result))
- {
- code_status = 100;
- message = "Create new user failed!";
- }
- else
- {
- code_status = 200;
- message = "Create new user success!";
- }
- return Json(new { code = code_status, msg = message });
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- }
- conn.Dispose();
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- public async Task<IActionResult> UpdateUser(long autoid, string user_id, string user_nm, string password, long paket_id, bool allow_cmd, int only_do)
- {
- var user_identity = new UserManagerAccessor(_httpContextAccessor);
- var conn = new SqlConnection(connectionstring);
- var companyId = user_identity.GetCompanyId();
- var userid = user_identity.GetUserId();
- var code_status = 0;
- var message = "";
- try
- {
- if (conn.State == ConnectionState.Closed)
- {
- conn.Open();
- }
- var p = new DynamicParameters();
- p.Add("@autoid", autoid, dbType: DbType.String, direction: ParameterDirection.Input);
- p.Add("@user_id", user_id, dbType: DbType.String, direction: ParameterDirection.Input);
- p.Add("@user_nm", user_nm, dbType: DbType.String, direction: ParameterDirection.Input);
- p.Add("@password", password, dbType: DbType.String, direction: ParameterDirection.Input);
- p.Add("@paket_id", paket_id, dbType: DbType.Int64, direction: ParameterDirection.Input);
- p.Add("@allow_cmd", allow_cmd, dbType: DbType.Boolean, direction: ParameterDirection.Input);
- p.Add("@only_do", only_do, dbType: DbType.Int32, direction: ParameterDirection.Input);
- //p.Add("@userid", userid, dbType: DbType.Int64, direction: ParameterDirection.Input);
- p.Add("@companyid", companyId, dbType: DbType.Int64, direction: ParameterDirection.Input);
- var result = await SqlMapper.QueryFirstOrDefaultAsync<string>(conn, "usp_cpanel_update_user", p, commandType: CommandType.StoredProcedure);
- if (string.IsNullOrEmpty(result))
- {
- code_status = 100;
- message = "Create new user failed!";
- }
- else
- {
- code_status = 200;
- message = "Create new user success!";
- }
- return Json(new { code = code_status, msg = message });
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- }
- conn.Dispose();
- }
- }
- [HttpPost]
- public IActionResult UpdatePassword(LoginUserChangePassword model)
- {
- try
- {
- if (ModelState.IsValid)
- {
- if (string.IsNullOrEmpty(model.old_password))
- {
- TempData["msg"] = "Old password cant empty!";
- return RedirectToAction("ChangePassword", "Cpanel");
- }
- else if (string.IsNullOrEmpty(model.new_password))
- {
- TempData["msg"] = "new password cant empty!";
- return RedirectToAction("ChangePassword", "Cpanel");
- }
- else if (model.new_password == model.old_password)
- {
- TempData["msg"] = "nothing has been changed!";
- return RedirectToAction("ChangePassword", "Cpanel");
- }
- else if (string.IsNullOrEmpty(model.confirm_password))
- {
- TempData["msg"] = "confirm password cant empty!";
- return RedirectToAction("ChangePassword", "Cpanel");
- }
- else if (model.confirm_password != model.new_password)
- {
- TempData["msg"] = "confirm password not same with new password!";
- return RedirectToAction("ChangePassword", "Cpanel");
- }
- else if (model.confirm_password.Length < 8 || model.new_password.Length < 8)
- {
- TempData["msg"] = "max length password 8!";
- return RedirectToAction("ChangePassword", "Cpanel");
- }
- else if (model.confirm_password.Contains(" ") || model.new_password.Contains(" "))
- {
- TempData["msg"] = "space in password not allowed!";
- return RedirectToAction("ChangePassword", "Cpanel");
- }
- else
- {
- var username = userIdentity.GetUserAkses();
- var db = new DBHelper(connectionstring);
- var p = new DynamicParameters();
- p.Add("@Username", username, DbType.String, ParameterDirection.Input);
- p.Add("@Password", model.old_password, DbType.String, ParameterDirection.Input);
- var result = db.ExecuteToModelSingle<LoginUserVts>("usp_login_fms", CommandType.StoredProcedure, p);
- if (result.user_nm == "")
- {
- TempData["msg"] = "Username or password incorrect!";
- return RedirectToAction("ChangePassword", "Cpanel");
- }
- else
- {
- var company_id = userIdentity.GetCompanyId();
- var pp = new DynamicParameters();
- pp.Add("@companyId", company_id, DbType.Int32, ParameterDirection.Input);
- pp.Add("@userId", username, DbType.String, ParameterDirection.Input);
- pp.Add("@password", model.old_password, DbType.String, ParameterDirection.Input);
- pp.Add("@new_password", model.new_password, DbType.String, ParameterDirection.Input);
- var msg = db.ExecuteScalar("usp_change_password", CommandType.StoredProcedure, pp);
- TempData["msg"] = msg;
- return RedirectToAction("ChangePassword", "Cpanel");
- }
- }
- }
- else
- {
- TempData["msg"] = "Error Model paramater";
- return RedirectToAction("ChangePassword", "Cpanel");
- }
- //return View("~/Views/Cpanel/ChangePassword.cshtml");
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #region UNITS
- public async Task<IActionResult> GetListCarMaster(DtParameters model)
- {
- var user_identity = new UserManagerAccessor(_httpContextAccessor);
- var conn = new SqlConnection(connectionstring);
- var _companyId = user_identity.GetCompanyId();
- var userid = user_identity.GetUserId();
- try
- {
- IEnumerable<LisCarMaster> result;
- var draw = model.Draw == 0 ? 1 : model.Draw;
- var start = model.Start == 0 ? 1 : model.Start;
- var length = model.Length == 0 ? 10 : model.Length;
- var search = string.IsNullOrEmpty(model.Search.Value) ? "" : model.Search.Value.ToString();
- var order = model.SortOrder;
- if (order.ToLower() == "rowno")
- order = "cm.autoid";
- if (conn.State == ConnectionState.Closed)
- {
- conn.Open();
- }
- var p = new DynamicParameters();
- p.Add("@StartCurrent", start, dbType: DbType.Int32, direction: ParameterDirection.Input);
- p.Add("@PageSize", length, dbType: DbType.Int32, direction: ParameterDirection.Input);
- p.Add("@SearchPhrase", search, dbType: DbType.String, direction: ParameterDirection.Input);
- p.Add("@SortOrder", order, dbType: DbType.String, direction: ParameterDirection.Input);
- p.Add("@UserId", userid, dbType: DbType.Int64, direction: ParameterDirection.Input);
- p.Add("@CompanyId", _companyId, dbType: DbType.Int64, direction: ParameterDirection.Input);
- p.Add("@RecordCount", dbType: DbType.Int64, direction: ParameterDirection.Output);
- result = await SqlMapper.QueryAsync<LisCarMaster>(conn, "usp_cpanel_list_car_master", p, commandType: CommandType.StoredProcedure);
- var recordsTotal = p.Get<Int64>("@RecordCount");
- var jsonData = new DtResults<LisCarMaster>
- {
- draw = model.Draw,
- recordsTotal = recordsTotal,
- recordsFiltered = recordsTotal,
- data = result.ToList()
- };
- return Json(jsonData);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- }
- conn.Dispose();
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement