Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Dapper;
  8. using Microsoft.AspNetCore.Authorization;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Extensions.Configuration;
  12. using FmsDashboard.Helper;
  13. using FmsDashboard.Models.Cpanel;
  14. using FmsDashboard.Models.Cpanel.User;
  15. using static FmsDashboard.Helper.JqueryDataTableModel;
  16. using FmsDashboard.Models.Login;
  17.  
  18. namespace FmsDashboard.Controllers
  19. {
  20.     [Authorize]
  21.     public class CpanelApiController : Controller// CPANEL API
  22.     {
  23.         private readonly string connectionstring = "";
  24.         private readonly UserManagerAccessor userIdentity;
  25.         readonly IHttpContextAccessor _httpContextAccessor;
  26.  
  27.         public CpanelApiController(IConfiguration config, IHttpContextAccessor httpContextAccessor)
  28.         {
  29.             connectionstring = config["ConnectionStrings:connStringEasygo"];
  30.             _httpContextAccessor = httpContextAccessor;
  31.             userIdentity = new UserManagerAccessor(httpContextAccessor);
  32.         }
  33.         public IActionResult Index()
  34.         {
  35.             return View();
  36.         }
  37.  
  38.         public async Task<IActionResult> GetListUser(DtParameters model)
  39.         {
  40.             var user_identity = new UserManagerAccessor(_httpContextAccessor);
  41.             var conn = new SqlConnection(connectionstring);
  42.             var _companyId = user_identity.GetCompanyId();
  43.             var userid = user_identity.GetUserId();
  44.             try
  45.             {
  46.                 IEnumerable<ListUser> result;
  47.                 var draw = model.Draw == 0 ? 1 : model.Draw;
  48.                 var start = model.Start == 0 ? 1 : model.Start;
  49.                 var length = model.Length == 0 ? 10 : model.Length;
  50.                 var search = string.IsNullOrEmpty(model.Search.Value) ? "" : model.Search.Value.ToString();
  51.                 var order = model.SortOrder;
  52.  
  53.                 if (order.ToLower() == "rowno")
  54.                     order = "ul.user_nm";
  55.  
  56.                 if (conn.State == ConnectionState.Closed)
  57.                 {
  58.                     conn.Open();
  59.                 }
  60.                 var p = new DynamicParameters();
  61.                 p.Add("@StartCurrent", start, dbType: DbType.Int32, direction: ParameterDirection.Input);
  62.                 p.Add("@PageSize", length, dbType: DbType.Int32, direction: ParameterDirection.Input);
  63.                 p.Add("@SearchPhrase", search, dbType: DbType.String, direction: ParameterDirection.Input);
  64.                 p.Add("@SortOrder", order, dbType: DbType.String, direction: ParameterDirection.Input);
  65.                 p.Add("@UserId", userid, dbType: DbType.Int64, direction: ParameterDirection.Input);
  66.                 p.Add("@CompanyId", _companyId, dbType: DbType.Int64, direction: ParameterDirection.Input);
  67.                 p.Add("@RecordCount", dbType: DbType.Int64, direction: ParameterDirection.Output);
  68.                 result = await SqlMapper.QueryAsync<ListUser>(conn, "usp_cpanel_list_user", p, commandType: CommandType.StoredProcedure);
  69.                 var recordsTotal = p.Get<Int64>("@RecordCount");
  70.                 var jsonData = new DtResults<ListUser>
  71.                 {
  72.                     draw = model.Draw,
  73.                     recordsTotal = recordsTotal,
  74.                     recordsFiltered = recordsTotal,
  75.                     data = result.ToList()
  76.                 };
  77.                 return Json(jsonData);
  78.             }
  79.             catch (Exception ex)
  80.             {
  81.  
  82.                 throw ex;
  83.             }
  84.             finally
  85.             {
  86.                 if (conn.State == ConnectionState.Open)
  87.                 {
  88.                     conn.Close();
  89.                 }
  90.                 conn.Dispose();
  91.             }
  92.         }
  93.  
  94.  
  95.         public async Task<IActionResult> CreateUser(string user_id, string user_nm, string password, long paket_id, bool allow_cmd, int only_do)
  96.         {
  97.             try
  98.             {
  99.                 var user_identity = new UserManagerAccessor(_httpContextAccessor);
  100.                 var conn = new SqlConnection(connectionstring);
  101.                 var companyId = user_identity.GetCompanyId();
  102.                 var userid = user_identity.GetUserId();
  103.                 var code_status = 0;
  104.                 var message = "";
  105.                 try
  106.                 {
  107.                     if (conn.State == ConnectionState.Closed)
  108.                     {
  109.                         conn.Open();
  110.                     }
  111.                     var p = new DynamicParameters();
  112.                     p.Add("@user_id", user_id, dbType: DbType.String, direction: ParameterDirection.Input);
  113.                     p.Add("@user_nm", user_nm, dbType: DbType.String, direction: ParameterDirection.Input);
  114.                     p.Add("@password", password, dbType: DbType.String, direction: ParameterDirection.Input);
  115.                     p.Add("@paket_id", paket_id, dbType: DbType.Int64, direction: ParameterDirection.Input);
  116.                     p.Add("@allow_cmd", allow_cmd, dbType: DbType.Boolean, direction: ParameterDirection.Input);
  117.                     p.Add("@only_do", only_do, dbType: DbType.Int32, direction: ParameterDirection.Input);
  118.                     //p.Add("@userid", userid, dbType: DbType.Int64, direction: ParameterDirection.Input);
  119.                     p.Add("@companyid", companyId, dbType: DbType.Int64, direction: ParameterDirection.Input);
  120.                     var result = await SqlMapper.QueryFirstOrDefaultAsync<string>(conn, "usp_cpanel_create_user", p, commandType: CommandType.StoredProcedure);
  121.                     if (string.IsNullOrEmpty(result))
  122.                     {
  123.                         code_status = 100;
  124.                         message = "Create new user failed!";
  125.                     }
  126.                     else
  127.                     {
  128.                         code_status = 200;
  129.                         message = "Create new user success!";
  130.                     }
  131.                     return Json(new { code = code_status, msg = message });
  132.                 }
  133.                 catch (Exception ex)
  134.                 {
  135.  
  136.                     throw ex;
  137.                 }
  138.                 finally
  139.                 {
  140.                     if (conn.State == ConnectionState.Open)
  141.                     {
  142.                         conn.Close();
  143.                     }
  144.                     conn.Dispose();
  145.                 }
  146.             }
  147.             catch (Exception)
  148.             {
  149.  
  150.                 throw;
  151.             }
  152.         }
  153.  
  154.         public async Task<IActionResult> UpdateUser(long autoid, string user_id, string user_nm, string password, long paket_id, bool allow_cmd, int only_do)
  155.         {
  156.             var user_identity = new UserManagerAccessor(_httpContextAccessor);
  157.             var conn = new SqlConnection(connectionstring);
  158.             var companyId = user_identity.GetCompanyId();
  159.             var userid = user_identity.GetUserId();
  160.             var code_status = 0;
  161.             var message = "";
  162.             try
  163.             {
  164.                 if (conn.State == ConnectionState.Closed)
  165.                 {
  166.                     conn.Open();
  167.                 }
  168.                 var p = new DynamicParameters();
  169.                 p.Add("@autoid", autoid, dbType: DbType.String, direction: ParameterDirection.Input);
  170.                 p.Add("@user_id", user_id, dbType: DbType.String, direction: ParameterDirection.Input);
  171.                 p.Add("@user_nm", user_nm, dbType: DbType.String, direction: ParameterDirection.Input);
  172.                 p.Add("@password", password, dbType: DbType.String, direction: ParameterDirection.Input);
  173.                 p.Add("@paket_id", paket_id, dbType: DbType.Int64, direction: ParameterDirection.Input);
  174.                 p.Add("@allow_cmd", allow_cmd, dbType: DbType.Boolean, direction: ParameterDirection.Input);
  175.                 p.Add("@only_do", only_do, dbType: DbType.Int32, direction: ParameterDirection.Input);
  176.                 //p.Add("@userid", userid, dbType: DbType.Int64, direction: ParameterDirection.Input);
  177.                 p.Add("@companyid", companyId, dbType: DbType.Int64, direction: ParameterDirection.Input);
  178.                 var result = await SqlMapper.QueryFirstOrDefaultAsync<string>(conn, "usp_cpanel_update_user", p, commandType: CommandType.StoredProcedure);
  179.                 if (string.IsNullOrEmpty(result))
  180.                 {
  181.                     code_status = 100;
  182.                     message = "Create new user failed!";
  183.                 }
  184.                 else
  185.                 {
  186.                     code_status = 200;
  187.                     message = "Create new user success!";
  188.                 }
  189.                 return Json(new { code = code_status, msg = message });
  190.             }
  191.             catch (Exception ex)
  192.             {
  193.  
  194.                 throw ex;
  195.             }
  196.             finally
  197.             {
  198.                 if (conn.State == ConnectionState.Open)
  199.                 {
  200.                     conn.Close();
  201.                 }
  202.                 conn.Dispose();
  203.             }
  204.         }
  205.  
  206.         [HttpPost]
  207.         public IActionResult UpdatePassword(LoginUserChangePassword model)
  208.         {
  209.             try
  210.             {
  211.                 if (ModelState.IsValid)
  212.                 {
  213.                     if (string.IsNullOrEmpty(model.old_password))
  214.                     {
  215.                         TempData["msg"] = "Old password cant empty!";
  216.                         return RedirectToAction("ChangePassword", "Cpanel");
  217.                     }
  218.                     else if (string.IsNullOrEmpty(model.new_password))
  219.                     {
  220.                         TempData["msg"] = "new password cant empty!";
  221.                         return RedirectToAction("ChangePassword", "Cpanel");
  222.                     }
  223.                     else if (model.new_password == model.old_password)
  224.                     {
  225.                         TempData["msg"] = "nothing has been changed!";
  226.                         return RedirectToAction("ChangePassword", "Cpanel");
  227.                     }
  228.                     else if (string.IsNullOrEmpty(model.confirm_password))
  229.                     {
  230.                         TempData["msg"] = "confirm password cant empty!";
  231.                         return RedirectToAction("ChangePassword", "Cpanel");
  232.                     }
  233.                     else if (model.confirm_password != model.new_password)
  234.                     {
  235.                         TempData["msg"] = "confirm password not same with new password!";
  236.                         return RedirectToAction("ChangePassword", "Cpanel");
  237.                     }
  238.                     else if (model.confirm_password.Length < 8 || model.new_password.Length < 8)
  239.                     {
  240.                         TempData["msg"] = "max length password 8!";
  241.                         return RedirectToAction("ChangePassword", "Cpanel");
  242.                     }
  243.                     else if (model.confirm_password.Contains(" ") || model.new_password.Contains(" "))
  244.                     {
  245.                         TempData["msg"] = "space in password not allowed!";
  246.                         return RedirectToAction("ChangePassword", "Cpanel");
  247.                     }
  248.                     else
  249.                     {
  250.                         var username = userIdentity.GetUserAkses();
  251.                         var db = new DBHelper(connectionstring);
  252.                         var p = new DynamicParameters();
  253.                         p.Add("@Username", username, DbType.String, ParameterDirection.Input);
  254.                         p.Add("@Password", model.old_password, DbType.String, ParameterDirection.Input);
  255.                         var result = db.ExecuteToModelSingle<LoginUserVts>("usp_login_fms", CommandType.StoredProcedure, p);
  256.                         if (result.user_nm == "")
  257.                         {
  258.                             TempData["msg"] = "Username or password incorrect!";
  259.                             return RedirectToAction("ChangePassword", "Cpanel");
  260.                         }
  261.                         else
  262.                         {
  263.                             var company_id = userIdentity.GetCompanyId();
  264.                             var pp = new DynamicParameters();
  265.                             pp.Add("@companyId", company_id, DbType.Int32, ParameterDirection.Input);
  266.                             pp.Add("@userId", username, DbType.String, ParameterDirection.Input);
  267.                             pp.Add("@password", model.old_password, DbType.String, ParameterDirection.Input);
  268.                             pp.Add("@new_password", model.new_password, DbType.String, ParameterDirection.Input);
  269.                             var msg = db.ExecuteScalar("usp_change_password", CommandType.StoredProcedure, pp);
  270.                             TempData["msg"] = msg;
  271.                             return RedirectToAction("ChangePassword", "Cpanel");
  272.                         }
  273.                     }
  274.                 }
  275.                 else
  276.                 {
  277.                     TempData["msg"] = "Error Model paramater";
  278.                     return RedirectToAction("ChangePassword", "Cpanel");
  279.                 }
  280.                 //return View("~/Views/Cpanel/ChangePassword.cshtml");
  281.             }
  282.             catch (Exception ex)
  283.             {
  284.  
  285.                 throw ex;
  286.             }
  287.         }
  288.  
  289.         #region UNITS
  290.         public async Task<IActionResult> GetListCarMaster(DtParameters model)
  291.         {
  292.             var user_identity = new UserManagerAccessor(_httpContextAccessor);
  293.             var conn = new SqlConnection(connectionstring);
  294.             var _companyId = user_identity.GetCompanyId();
  295.             var userid = user_identity.GetUserId();
  296.             try
  297.             {
  298.                 IEnumerable<LisCarMaster> result;
  299.                 var draw = model.Draw == 0 ? 1 : model.Draw;
  300.                 var start = model.Start == 0 ? 1 : model.Start;
  301.                 var length = model.Length == 0 ? 10 : model.Length;
  302.                 var search = string.IsNullOrEmpty(model.Search.Value) ? "" : model.Search.Value.ToString();
  303.                 var order = model.SortOrder;
  304.  
  305.                 if (order.ToLower() == "rowno")
  306.                     order = "cm.autoid";
  307.  
  308.                 if (conn.State == ConnectionState.Closed)
  309.                 {
  310.                     conn.Open();
  311.                 }
  312.                 var p = new DynamicParameters();
  313.                 p.Add("@StartCurrent", start, dbType: DbType.Int32, direction: ParameterDirection.Input);
  314.                 p.Add("@PageSize", length, dbType: DbType.Int32, direction: ParameterDirection.Input);
  315.                 p.Add("@SearchPhrase", search, dbType: DbType.String, direction: ParameterDirection.Input);
  316.                 p.Add("@SortOrder", order, dbType: DbType.String, direction: ParameterDirection.Input);
  317.                 p.Add("@UserId", userid, dbType: DbType.Int64, direction: ParameterDirection.Input);
  318.                 p.Add("@CompanyId", _companyId, dbType: DbType.Int64, direction: ParameterDirection.Input);
  319.                 p.Add("@RecordCount", dbType: DbType.Int64, direction: ParameterDirection.Output);
  320.                 result = await SqlMapper.QueryAsync<LisCarMaster>(conn, "usp_cpanel_list_car_master", p, commandType: CommandType.StoredProcedure);
  321.                 var recordsTotal = p.Get<Int64>("@RecordCount");
  322.                 var jsonData = new DtResults<LisCarMaster>
  323.                 {
  324.                     draw = model.Draw,
  325.                     recordsTotal = recordsTotal,
  326.                     recordsFiltered = recordsTotal,
  327.                     data = result.ToList()
  328.                 };
  329.                 return Json(jsonData);
  330.             }
  331.             catch (Exception ex)
  332.             {
  333.  
  334.                 throw ex;
  335.             }
  336.             finally
  337.             {
  338.                 if (conn.State == ConnectionState.Open)
  339.                 {
  340.                     conn.Close();
  341.                 }
  342.                 conn.Dispose();
  343.             }
  344.         }
  345.         #endregion
  346.     }
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement