Ejemplo n.º 1
0
        private void SetUserType_Role_Status(Dictionary <string, string> _d, int nProvUserId)
        {
            this.UserCredentials = new UserCredentials()
            {
                Email  = _d.ContainsKey(FormConstants.email) ? _d[FormConstants.email] : string.Empty,
                Phone  = _d.ContainsKey(FormConstants.phprimary) ? _d[FormConstants.phprimary] : string.Empty,
                Pwd    = this.GetRandomPwd(),
                Name   = _d.ContainsKey(FormConstants.fullname) ? _d[FormConstants.fullname] : (_d.ContainsKey(FormConstants.email) ? _d[FormConstants.email] : _d[FormConstants.phprimary]),
                UserId = nProvUserId
            };
            this.AddOrChange(_d, FormConstants.pwd, this.UserCredentials.Pwd);/*(this.UserCredentials.Pwd + this.UserCredentials.Email).ToMD5Hash());*/

            if (!_d.ContainsKey(FormConstants.usertype))
            {
                List <EbUserType> u_types = this.UserTypeToRole.FindAll(e => e.bVisible);
                if (u_types.Count == 1)
                {
                    _d.Add(FormConstants.usertype, Convert.ToString(u_types[0].iValue));
                }
            }

            if (_d.ContainsKey(FormConstants.usertype))
            {
                int        u_type = Convert.ToInt32(_d[FormConstants.usertype]);
                EbUserType ebTyp  = this.UserTypeToRole.Find(e => e.iValue == u_type && e.bVisible);
                if (ebTyp != null)
                {
                    if (ebTyp.ApprovalRequired)
                    {
                        this.AddOrChange(_d, FormConstants.statusid, ((int)EbUserStatus.Unapproved).ToString());
                    }
                    else if (ebTyp.Roles != null && ebTyp.Roles.Count > 0)
                    {
                        if (_d.TryGetValue(FormConstants.primary_role, out string priRole_s) && int.TryParse(priRole_s, out int priRole_i) && !ebTyp.Roles.Contains(priRole_i) && priRole_i > 100)
                        {
                            ebTyp.Roles.Add(priRole_i);
                        }
                        this.AddOrChange(_d, FormConstants.roles, string.Join(CharConstants.COMMA, ebTyp.Roles));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public override bool ParameterizeControl(ParameterizeCtrl_Params args, string crudContext)
        {
            if (!this.CreateOnlyIf_b)
            {
                return(false);
            }
            string c                       = string.Empty;
            bool   doNotUpdate             = false;
            bool   insertOnUpdate          = false;
            Dictionary <string, string> _d = JsonConvert.DeserializeObject <Dictionary <string, string> >(Convert.ToString(args.cField.F));
            int nProvUserId                = 0;
            int flag                       = 0;

            if ((_d.ContainsKey(FormConstants.email) && _d[FormConstants.email].Trim() != string.Empty) || (_d.ContainsKey(FormConstants.phprimary) && _d[FormConstants.phprimary].Trim() != string.Empty))
            {
                nProvUserId = this.GetUserIdByEmailOrPhone(args.DataDB, _d, ref flag, args.ins, args.ocF);
            }
            else
            {
                return(false);
            }
            if (args.ins)
            {
                if (nProvUserId > 0)// user already exists
                {
                    if (!this.AllowExistingUser)
                    {
                        this.ThrowExistingUserException(_d, flag);
                    }

                    args._cols += args.cField.Name + CharConstants.COMMA + CharConstants.SPACE;
                    args._vals += CharConstants.AT + args.cField.Name + CharConstants.UNDERSCORE + args.i + CharConstants.COMMA + CharConstants.SPACE;
                    args.param.Add(args.DataDB.GetNewParameter(args.cField.Name + CharConstants.UNDERSCORE + args.i, (EbDbTypes)args.cField.Type, nProvUserId));
                    args.i++;
                    doNotUpdate = true;
                }
                else
                {
                    this.SetUserType_Role_Status(_d, nProvUserId);
                }
            }
            else
            {
                int oProvUserId = Convert.ToInt32(args.ocF.Value);
                Dictionary <string, string> _od = JsonConvert.DeserializeObject <Dictionary <string, string> >(Convert.ToString(args.ocF.F));
                if (_od.ContainsKey(FormConstants.id) && (oProvUserId == nProvUserId))// means user created by this control
                {
                    this.AddOrChange(_d, FormConstants.id, _od[FormConstants.id]);
                    int oCreUserId = Convert.ToInt32(_od[FormConstants.id]);
                    if (oCreUserId != nProvUserId || !_d.ContainsKey(FormConstants.email))
                    {
                        this.AddOrChange(_d, FormConstants.email, _od[FormConstants.email]);// remove this line if you want to edit email via prov user ctrl
                    }
                    if (oCreUserId != nProvUserId || !_d.ContainsKey(FormConstants.phprimary))
                    {
                        this.AddOrChange(_d, FormConstants.phprimary, _od[FormConstants.phprimary]);
                    }
                    this.AddOrChange(_d, FormConstants.usertype, _od[FormConstants.usertype]);
                    int oldStatus = Convert.ToInt32(_od[FormConstants.statusid]);
                    if (!(_d.TryGetValue(FormConstants.statusid, out string newStatus_s) && int.TryParse(newStatus_s, out int newStatus_i) && newStatus_i != oldStatus))//if not status changed
                    {
                        this.AddOrChange(_d, FormConstants.statusid, Convert.ToString(oldStatus + 100));
                    }
                    if (_od.ContainsKey(FormConstants.locConstraint))
                    {
                        this.AddOrChange(_d, FormConstants.locConstraint, _od[FormConstants.locConstraint]);
                    }

                    if (oldStatus == (int)EbUserStatus.Unapproved)
                    {
                        if (args.usr.Roles.Contains(SystemRoles.SolutionOwner.ToString()) || args.usr.Roles.Contains(SystemRoles.SolutionAdmin.ToString()))
                        {
                            int        u_type = Convert.ToInt32(_od[FormConstants.usertype]);
                            EbUserType ebTyp  = this.UserTypeToRole.Find(e => e.iValue == u_type && e.bVisible);
                            if (ebTyp != null && ebTyp.Roles != null && ebTyp.Roles.Count > 0)
                            {
                                if (_d.TryGetValue(FormConstants.primary_role, out string priRole_s) && int.TryParse(priRole_s, out int priRole_i) && !ebTyp.Roles.Contains(priRole_i) && priRole_i > 100)
                                {
                                    ebTyp.Roles.Add(priRole_i);
                                }
                                this.AddOrChange(_d, FormConstants.statusid, ((int)EbUserStatus.Active).ToString());
                                this.AddOrChange(_d, FormConstants.roles, string.Join(CharConstants.COMMA, ebTyp.Roles));
                            }
                        }
                    }

                    foreach (KeyValuePair <string, string> item in _od)
                    {
                        if (!_d.ContainsKey(item.Key))
                        {
                            string val = item.Value;
                            if (item.Key == FormConstants.roles)
                            {
                                if (_d.TryGetValue(FormConstants.primary_role, out string priRole_s) && int.TryParse(priRole_s, out int priRole_i) && priRole_i > 100)
                                {
                                    List <string> st = string.IsNullOrWhiteSpace(item.Value) ? new List <string>() : item.Value.Split(",").ToList();
                                    if (!st.Contains(priRole_s))
                                    {
                                        if (st.Count > 0)// remove old primary roles
                                        {
                                            string      Qry = $"SELECT id FROM eb_roles WHERE is_primary='T' AND COALESCE(eb_del, 'F')='F' AND id IN(SELECT UNNEST(STRING_TO_ARRAY('{item.Value}', ',')::INTEGER[]));";
                                            EbDataTable dt  = args.DataDB.DoQuery(Qry);
                                            foreach (EbDataRow dr in dt.Rows)
                                            {
                                                string old_rid = Convert.ToString(dr[0]);
                                                if (st.Contains(old_rid))
                                                {
                                                    st.Remove(old_rid);
                                                }
                                            }
                                        }
                                        st.Add(priRole_s);// add new primary role
                                        val = st.Join(",");
                                    }
                                }
                            }
                            _d.Add(item.Key, val);
                        }
                    }
                }
                else
                {
                    doNotUpdate = true;
                    if (nProvUserId > 1 && oProvUserId != nProvUserId)
                    {
                        if (!this.AllowExistingUser)
                        {
                            this.ThrowExistingUserException(_d, flag);
                        }

                        args._colvals += string.Concat(args.cField.Name, CharConstants.EQUALS, CharConstants.AT, args.cField.Name, CharConstants.UNDERSCORE, args.i, CharConstants.COMMA, CharConstants.SPACE);
                        args.param.Add(args.DataDB.GetNewParameter(args.cField.Name + CharConstants.UNDERSCORE + args.i, (EbDbTypes)args.cField.Type, nProvUserId));
                        args.i++;
                    }
                    else if (nProvUserId <= 0)
                    {
                        insertOnUpdate = true;
                        this.SetUserType_Role_Status(_d, nProvUserId);
                    }
                }
            }
            if (!doNotUpdate || insertOnUpdate)
            {
                this.MergeConstraints(_d, nProvUserId);

                string p_email = string.Empty, p_phone = string.Empty;
                for (int k = 0; k < this.FuncParam.Length; k++, args.i++)
                {
                    object _value = this.FuncParam[k].Value;
                    if (_d.ContainsKey(this.FuncParam[k].Name) && !string.IsNullOrEmpty(_d[this.FuncParam[k].Name]))
                    {
                        _value = _d[this.FuncParam[k].Name];
                        if (this.FuncParam[k].Name.Equals(FormConstants.email))
                        {
                            p_email = string.Concat(CharConstants.COLON, this.FuncParam[k].Name, CharConstants.UNDERSCORE, args.i);
                        }
                        if (this.FuncParam[k].Name.Equals(FormConstants.phprimary))
                        {
                            p_phone = string.Concat(CharConstants.COLON, this.FuncParam[k].Name, CharConstants.UNDERSCORE, args.i);
                        }
                    }
                    c += string.Concat(CharConstants.COLON, this.FuncParam[k].Name, CharConstants.UNDERSCORE, args.i, CharConstants.COMMA, CharConstants.SPACE);
                    if (_value == DBNull.Value)
                    {
                        var p = args.DataDB.GetNewParameter(this.FuncParam[k].Name + CharConstants.UNDERSCORE + args.i, this.FuncParam[k].Type);
                        p.Value = _value;
                        args.param.Add(p);
                    }
                    else
                    {
                        args.param.Add(args.DataDB.GetNewParameter(this.FuncParam[k].Name + CharConstants.UNDERSCORE + args.i, this.FuncParam[k].Type, _value));
                    }
                }

                args._extqry += this.GetSaveQuery(args.ins, c.Substring(0, c.Length - 2), args.tbl, p_email, p_phone, insertOnUpdate);
            }
            return(true);
        }